Adding message parts |
You may create multi-part MIME messages by adding one MIME message to another using the MimeMessage#addPart method.
Example
// body text for plain text part String plainText = "This is plain text";
// body text for html text part String htmlText = "<b>This is HTML text</b>";
// create parent message MimeMessage contents = new MimeMessage();
// set Content-Type header to multipart/alternative contents.setContentType("multipart/alternative");
// create plain text part and add to parent try { MimeMessage plainMessage = new MimeMessage(); plainMessage.setContentType("text/plain"); plainMessage.setBody(plainText); contents.addPart(plainMessage); } catch (Exception e) { System.out.println(e); }
// create html text part and add to parent try { MimeMessage htmlMessage = new MimeMessage(); htmlMessage.setContentType("text/html"); htmlMessage.setBody(htmlText); contents.addPart(htmlMessage); } catch (Exception e) { System.out.println(e); }
|