Java Reference
In-Depth Information
Within the JavaMail API you can create a Multipurpose Internet Mail Extensions
(MIME) e-mail. This type of message allows it to contain different body parts. In the
example, a plain text body part is generated (which contains the text that the e-mail dis-
plays), and then two attachment body parts containing the attachments you are trying to
send are created. Depending on the type of attachments, the Java API will automatic-
ally choose an appropriate encoding for the attachment body part.
After each of the body parts are created, they are combined by creating a
MultiPart object and adding each individual part (the plain text and the attach-
ments) to it. Once the MultiPart object has been assembled to contain all the parts,
it is assigned as the content of the MimeMessage and sent (just like in Recipe 19-2).
19-4. Sending an HTML E-Mail
Problem
You want to send an e-mail that contains HTML.
Solution
You specify the content type of the e-mail as text / html and send a string of HTML
as the message body. In the following example, an e-mail is constructed using HTML
content and then it is sent.
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new
InternetAddress(to));
message.setSubject("Subject Test");
// Create Mime Content
MimeBodyPart messageBodyPart = new MimeBodyPart();
String html = "<H1>Important Message</H1>" +
"<b>This is an important
message...</b>"+
"<br/><br/>" +
Search WWH ::




Custom Search