or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-mail-operations.mdevent-handling-system.mdindex.mdinternet-mail-mime.mdmessage-search-filtering.mdstore-folder-management.mdutility-classes-streams.md

internet-mail-mime.mddocs/

0

# Internet Mail (MIME) Support

1

2

Internet Mail support provides comprehensive MIME message creation, multipart handling, internet address management, and header processing for standards-compliant email handling.

3

4

## MIME Message Implementation

5

6

The MimeMessage class provides a complete implementation of MIME-formatted email messages.

7

8

```java { .api }

9

public class MimeMessage extends Message implements MimePart {

10

// Constructors

11

public MimeMessage(Session session);

12

public MimeMessage(Session session, InputStream is) throws MessagingException;

13

public MimeMessage(MimeMessage source) throws MessagingException;

14

15

// Text content methods

16

public void setText(String text) throws MessagingException;

17

public void setText(String text, String charset) throws MessagingException;

18

public void setText(String text, String charset, String subtype) throws MessagingException;

19

20

// Content management

21

public void setContent(Object o, String type) throws MessagingException;

22

public void setContent(Multipart mp) throws MessagingException;

23

24

// Message preparation

25

public void saveChanges() throws MessagingException;

26

public void updateHeaders() throws MessagingException;

27

28

// MIME-specific methods

29

public String getEncoding() throws MessagingException;

30

public void setDescription(String description) throws MessagingException;

31

public void setDescription(String description, String charset) throws MessagingException;

32

33

// Header management (inherited from MimePart)

34

public void setHeader(String name, String value) throws MessagingException;

35

public void addHeader(String name, String value) throws MessagingException;

36

public void removeHeader(String name) throws MessagingException;

37

public String[] getHeader(String name) throws MessagingException;

38

public Enumeration<Header> getAllHeaders() throws MessagingException;

39

public Enumeration<Header> getMatchingHeaders(String[] names) throws MessagingException;

40

public Enumeration<Header> getNonMatchingHeaders(String[] names) throws MessagingException;

41

}

42

```

43

44

### MIME Message Usage Example

45

46

```java

47

import jakarta.mail.*;

48

import jakarta.mail.internet.*;

49

50

// Create MIME message

51

MimeMessage message = new MimeMessage(session);

52

53

// Set basic headers

54

message.setFrom(new InternetAddress("sender@example.com", "Sender Name"));

55

message.setRecipients(Message.RecipientType.TO,

56

InternetAddress.parse("recipient@example.com"));

57

message.setSubject("MIME Message Example", "UTF-8");

58

59

// Simple text content

60

message.setText("Hello, this is a simple text message", "UTF-8");

61

62

// HTML content

63

message.setContent("<h1>Hello</h1><p>This is an HTML message</p>", "text/html; charset=utf-8");

64

65

// Custom headers

66

message.setHeader("X-Priority", "1");

67

message.setHeader("X-Mailer", "My Mail Client");

68

69

// Save changes before sending

70

message.saveChanges();

71

Transport.send(message);

72

```

73

74

## Internet Address Management

75

76

The InternetAddress class handles RFC822-compliant email addresses with personal names.

77

78

```java { .api }

79

public class InternetAddress extends Address implements Cloneable {

80

// Constructors

81

public InternetAddress();

82

public InternetAddress(String address) throws AddressException;

83

public InternetAddress(String address, boolean strict) throws AddressException;

84

public InternetAddress(String address, String personal) throws AddressException;

85

public InternetAddress(String address, String personal, String charset) throws AddressException;

86

87

// Static parsing methods

88

public static InternetAddress[] parse(String addresslist) throws AddressException;

89

public static InternetAddress[] parse(String addresslist, boolean strict) throws AddressException;

90

public static InternetAddress[] parseHeader(String addresslist, boolean strict) throws AddressException;

91

92

// Static utility methods

93

public static String toString(Address[] addresses);

94

public static String toString(Address[] addresses, int used);

95

96

// Address components

97

public String getAddress();

98

public void setAddress(String address) throws AddressException;

99

public String getPersonal();

100

public void setPersonal(String name) throws UnsupportedEncodingException;

101

public void setPersonal(String name, String charset) throws UnsupportedEncodingException;

102

103

// Validation

104

public void validate() throws AddressException;

105

public static void validate(Address[] addresses) throws AddressException;

106

107

// Address operations

108

public Object clone();

109

public boolean equals(Object a);

110

public int hashCode();

111

public String toString();

112

public String toUnicodeString();

113

}

114

```

115

116

### Internet Address Usage Example

117

118

```java

119

import jakarta.mail.internet.*;

120

121

// Simple address creation

122

InternetAddress simple = new InternetAddress("user@example.com");

123

124

// Address with personal name

125

InternetAddress withName = new InternetAddress("user@example.com", "John Doe");

126

127

// Address with Unicode personal name

128

InternetAddress unicode = new InternetAddress("user@example.com", "José García", "UTF-8");

129

130

// Parse address lists

131

InternetAddress[] addresses = InternetAddress.parse("user1@example.com, John Doe <user2@example.com>");

132

133

// Parse with strict validation

134

try {

135

InternetAddress[] strict = InternetAddress.parse("invalid-address", true);

136

} catch (AddressException e) {

137

System.out.println("Invalid address format: " + e.getMessage());

138

}

139

140

// Convert addresses back to string

141

String addressList = InternetAddress.toString(addresses);

142

143

// Validate addresses

144

try {

145

InternetAddress.validate(addresses);

146

System.out.println("All addresses are valid");

147

} catch (AddressException e) {

148

System.out.println("Invalid address found: " + e.getMessage());

149

}

150

```

151

152

## News Address Support

153

154

The NewsAddress class handles Usenet newsgroup addresses.

155

156

```java { .api }

157

public class NewsAddress extends Address {

158

// Constructors

159

public NewsAddress();

160

public NewsAddress(String newsgroup);

161

public NewsAddress(String newsgroup, String host);

162

163

// Static parsing methods

164

public static NewsAddress[] parse(String addresslist) throws AddressException;

165

166

// Address components

167

public String getNewsgroup();

168

public void setNewsgroup(String newsgroup);

169

public String getHost();

170

public void setHost(String host);

171

172

// Address operations

173

public String toString();

174

public boolean equals(Object a);

175

public int hashCode();

176

}

177

```

178

179

## Multipart Content Handling

180

181

The MimeMultipart class manages multipart MIME content with multiple body parts.

182

183

```java { .api }

184

public class MimeMultipart extends Multipart {

185

// Constructors

186

public MimeMultipart();

187

public MimeMultipart(String subtype);

188

public MimeMultipart(DataSource ds) throws MessagingException;

189

190

// Content management

191

public void setSubType(String subtype);

192

public int getCount() throws MessagingException;

193

public BodyPart getBodyPart(int index) throws MessagingException;

194

public void addBodyPart(BodyPart part) throws MessagingException;

195

public void addBodyPart(BodyPart part, int index) throws MessagingException;

196

public boolean removeBodyPart(BodyPart part) throws MessagingException;

197

public void removeBodyPart(int index) throws MessagingException;

198

199

// Serialization

200

public void writeTo(OutputStream os) throws IOException, MessagingException;

201

202

// Preamble and epilogue

203

public String getPreamble() throws MessagingException;

204

public void setPreamble(String preamble) throws MessagingException;

205

}

206

```

207

208

### MIME Body Part

209

210

```java { .api }

211

public class MimeBodyPart extends BodyPart implements MimePart {

212

// Constructors

213

public MimeBodyPart();

214

public MimeBodyPart(InputStream is) throws MessagingException;

215

public MimeBodyPart(InternetHeaders headers, byte[] content) throws MessagingException;

216

217

// Content methods

218

public void setText(String text) throws MessagingException;

219

public void setText(String text, String charset) throws MessagingException;

220

public void setText(String text, String charset, String subtype) throws MessagingException;

221

public void setContent(Object o, String type) throws MessagingException;

222

public void setContent(Multipart mp) throws MessagingException;

223

224

// File attachment methods

225

public void attachFile(File file) throws IOException, MessagingException;

226

public void attachFile(String file) throws IOException, MessagingException;

227

public void attachFile(File file, String contentType, String encoding) throws IOException, MessagingException;

228

229

// Filename methods

230

public String getFileName() throws MessagingException;

231

public void setFileName(String filename) throws MessagingException;

232

233

// Disposition methods

234

public String getDisposition() throws MessagingException;

235

public void setDisposition(String disposition) throws MessagingException;

236

237

// Description methods

238

public String getDescription() throws MessagingException;

239

public void setDescription(String description) throws MessagingException;

240

public void setDescription(String description, String charset) throws MessagingException;

241

}

242

```

243

244

### Multipart Usage Example

245

246

```java

247

import jakarta.mail.internet.*;

248

import jakarta.mail.*;

249

import java.io.File;

250

251

// Create multipart message

252

MimeMessage message = new MimeMessage(session);

253

message.setFrom(new InternetAddress("sender@example.com"));

254

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com"));

255

message.setSubject("Multipart Message with Attachment");

256

257

// Create multipart container

258

MimeMultipart multipart = new MimeMultipart("mixed");

259

260

// Text part

261

MimeBodyPart textPart = new MimeBodyPart();

262

textPart.setText("Please find the attached document.", "UTF-8");

263

multipart.addBodyPart(textPart);

264

265

// File attachment

266

MimeBodyPart attachmentPart = new MimeBodyPart();

267

attachmentPart.attachFile(new File("document.pdf"));

268

attachmentPart.setFileName("Important Document.pdf");

269

attachmentPart.setDisposition(Part.ATTACHMENT);

270

multipart.addBodyPart(attachmentPart);

271

272

// Set multipart content

273

message.setContent(multipart);

274

message.saveChanges();

275

276

// Alternative: HTML with inline images

277

MimeMultipart htmlMultipart = new MimeMultipart("related");

278

279

// HTML part

280

MimeBodyPart htmlPart = new MimeBodyPart();

281

htmlPart.setContent("<html><body><h1>Hello</h1><img src='cid:image1'></body></html>", "text/html; charset=utf-8");

282

htmlMultipart.addBodyPart(htmlPart);

283

284

// Inline image

285

MimeBodyPart imagePart = new MimeBodyPart();

286

imagePart.attachFile(new File("logo.png"));

287

imagePart.setDisposition(Part.INLINE);

288

imagePart.setHeader("Content-ID", "<image1>");

289

htmlMultipart.addBodyPart(imagePart);

290

```

291

292

## MIME Header Processing

293

294

The InternetHeaders class manages RFC822 message headers.

295

296

```java { .api }

297

public class InternetHeaders {

298

// Constructors

299

public InternetHeaders();

300

public InternetHeaders(InputStream is) throws MessagingException;

301

302

// Header access

303

public String[] getHeader(String name);

304

public String getHeader(String name, String delimiter);

305

public Enumeration<Header> getAllHeaders();

306

public Enumeration<Header> getMatchingHeaders(String[] names);

307

public Enumeration<Header> getNonMatchingHeaders(String[] names);

308

309

// Header modification

310

public void addHeader(String name, String value);

311

public void setHeader(String name, String value);

312

public void removeHeader(String name);

313

314

// Header operations

315

public void load(InputStream is) throws MessagingException;

316

}

317

```

318

319

### Header Tokenizer

320

321

The HeaderTokenizer class parses RFC822 header values into tokens.

322

323

```java { .api }

324

public class HeaderTokenizer {

325

// Token types

326

public static final int ATOM = -1;

327

public static final int QUOTEDSTRING = -2;

328

public static final int COMMENT = -3;

329

public static final int EOF = -4;

330

331

// Constructors

332

public HeaderTokenizer(String header);

333

public HeaderTokenizer(String header, String delimiters);

334

public HeaderTokenizer(String header, String delimiters, boolean skipComments);

335

336

// Token parsing

337

public Token next() throws ParseException;

338

public Token peek() throws ParseException;

339

public String getRemainder();

340

341

// Token class

342

public static class Token {

343

public int getType();

344

public String getValue();

345

}

346

}

347

```

348

349

### Header Processing Example

350

351

```java

352

import jakarta.mail.internet.*;

353

354

// Parse Content-Type header

355

String contentTypeHeader = "text/html; charset=utf-8; boundary=xyz123";

356

HeaderTokenizer tokenizer = new HeaderTokenizer(contentTypeHeader, ";");

357

358

HeaderTokenizer.Token token = tokenizer.next();

359

String primaryType = token.getValue(); // "text/html"

360

361

while ((token = tokenizer.next()).getType() != HeaderTokenizer.EOF) {

362

if (token.getType() == HeaderTokenizer.ATOM) {

363

String param = token.getValue();

364

// Process parameter

365

}

366

}

367

368

// Work with InternetHeaders

369

InternetHeaders headers = new InternetHeaders();

370

headers.setHeader("Content-Type", "text/plain; charset=utf-8");

371

headers.addHeader("X-Custom-Header", "custom-value");

372

373

// Get all headers

374

Enumeration<Header> allHeaders = headers.getAllHeaders();

375

while (allHeaders.hasMoreElements()) {

376

Header header = allHeaders.nextElement();

377

System.out.println(header.getName() + ": " + header.getValue());

378

}

379

```

380

381

## Content Type and Disposition Handling

382

383

### Content Type

384

385

```java { .api }

386

public class ContentType {

387

// Constructors

388

public ContentType();

389

public ContentType(String s) throws ParseException;

390

public ContentType(String primaryType, String subType, ParameterList list);

391

392

// Type access

393

public String getPrimaryType();

394

public String getSubType();

395

396

// Parameter access

397

public String getParameter(String name);

398

public ParameterList getParameterList();

399

public void setParameter(String name, String value);

400

public void setParameterList(ParameterList list);

401

402

// Type operations

403

public String getBaseType();

404

public boolean match(ContentType cType);

405

public boolean match(String s);

406

public String toString();

407

}

408

```

409

410

### Content Disposition

411

412

```java { .api }

413

public class ContentDisposition {

414

// Constructors

415

public ContentDisposition();

416

public ContentDisposition(String disposition) throws ParseException;

417

public ContentDisposition(String disposition, ParameterList list);

418

419

// Disposition access

420

public String getDisposition();

421

public void setDisposition(String disposition);

422

423

// Parameter access

424

public String getParameter(String name);

425

public ParameterList getParameterList();

426

public void setParameter(String name, String value);

427

public void setParameterList(ParameterList list);

428

429

// String representation

430

public String toString();

431

}

432

```

433

434

### Parameter List

435

436

```java { .api }

437

public class ParameterList {

438

// Constructors

439

public ParameterList();

440

public ParameterList(String s) throws ParseException;

441

442

// Parameter access

443

public int size();

444

public String get(String name);

445

public void set(String name, String value);

446

public void set(String name, String value, String charset);

447

public void remove(String name);

448

public Enumeration<String> getNames();

449

450

// String representation

451

public String toString();

452

public String toString(int used);

453

}

454

```

455

456

### Content Type Usage Example

457

458

```java

459

import jakarta.mail.internet.*;

460

461

// Parse Content-Type

462

ContentType contentType = new ContentType("text/html; charset=utf-8; boundary=xyz123");

463

System.out.println("Primary type: " + contentType.getPrimaryType()); // "text"

464

System.out.println("Sub type: " + contentType.getSubType()); // "html"

465

System.out.println("Charset: " + contentType.getParameter("charset")); // "utf-8"

466

467

// Create new Content-Type

468

ContentType newType = new ContentType("application", "pdf", null);

469

newType.setParameter("name", "document.pdf");

470

471

// Parse Content-Disposition

472

ContentDisposition disposition = new ContentDisposition("attachment; filename=\"document.pdf\"");

473

System.out.println("Disposition: " + disposition.getDisposition()); // "attachment"

474

System.out.println("Filename: " + disposition.getParameter("filename")); // "document.pdf"

475

476

// Work with parameters

477

ParameterList params = new ParameterList();

478

params.set("charset", "utf-8");

479

params.set("boundary", "----=_NextPart_000_0001");

480

System.out.println("Parameters: " + params.toString());

481

```

482

483

## MIME Utility Functions

484

485

The MimeUtility class provides encoding/decoding utilities for MIME content.

486

487

```java { .api }

488

public class MimeUtility {

489

// Text encoding/decoding

490

public static String encodeText(String text) throws UnsupportedEncodingException;

491

public static String encodeText(String text, String charset, String encoding) throws UnsupportedEncodingException;

492

public static String decodeText(String etext) throws UnsupportedEncodingException;

493

494

// Word encoding/decoding

495

public static String encodeWord(String word) throws UnsupportedEncodingException;

496

public static String encodeWord(String word, String charset, String encoding) throws UnsupportedEncodingException;

497

public static String decodeWord(String eword) throws ParseException, UnsupportedEncodingException;

498

499

// Stream encoding/decoding

500

public static InputStream encode(InputStream is, String encoding) throws MessagingException;

501

public static OutputStream encode(OutputStream os, String encoding) throws MessagingException;

502

public static InputStream decode(InputStream is, String encoding) throws MessagingException;

503

504

// Charset utilities

505

public static String getDefaultJavaCharset();

506

public static String getDefaultMIMECharset();

507

public static String mimeCharset(String charset);

508

public static String javaCharset(String charset);

509

510

// Quote utilities

511

public static String quote(String word, String specials);

512

public static String unfold(String s);

513

public static String fold(int used, String s);

514

}

515

```

516

517

### MIME Utility Usage Example

518

519

```java

520

import jakarta.mail.internet.*;

521

522

// Encode non-ASCII text for headers

523

String encodedSubject = MimeUtility.encodeText("Héllo Wörld", "UTF-8", "B");

524

message.setSubject(encodedSubject);

525

526

// Decode encoded text

527

String decodedText = MimeUtility.decodeText("=?UTF-8?B?SMOpbGxvIFfDtnJsZA==?=");

528

529

// Encode filename for attachment

530

String encodedFilename = MimeUtility.encodeWord("résumé.pdf", "UTF-8", "Q");

531

bodyPart.setFileName(encodedFilename);

532

533

// Work with streams

534

InputStream encodedStream = MimeUtility.encode(inputStream, "base64");

535

InputStream decodedStream = MimeUtility.decode(encodedStream, "base64");

536

537

// Charset conversion

538

String mimeCharset = MimeUtility.mimeCharset("UTF-8");

539

String javaCharset = MimeUtility.javaCharset("iso-8859-1");

540

541

// Text folding for long headers

542

String longHeader = "This is a very long header value that needs to be folded";

543

String foldedHeader = MimeUtility.fold(12, longHeader);

544

```

545

546

## Mail Date Formatting

547

548

The MailDateFormat class provides RFC822-compliant date formatting.

549

550

```java { .api }

551

public class MailDateFormat extends SimpleDateFormat {

552

// Constructors

553

public MailDateFormat();

554

555

// Date formatting (inherited from SimpleDateFormat)

556

public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos);

557

public Date parse(String text, ParsePosition pos);

558

}

559

```

560

561

### Date Formatting Example

562

563

```java

564

import jakarta.mail.internet.MailDateFormat;

565

import java.util.Date;

566

567

// Format date for mail headers

568

MailDateFormat mailDateFormat = new MailDateFormat();

569

String formattedDate = mailDateFormat.format(new Date());

570

message.setHeader("Date", formattedDate);

571

572

// Parse mail date

573

Date parsedDate = mailDateFormat.parse("Mon, 01 Jan 2024 12:00:00 +0000");

574

```