Java Reference
In-Depth Information
"<i>Be sure to code your Java
today!</i>" +
"<H2>It is the right thing to do!</H2>";
messageBodyPart.setContent(html, "text/html;
charset=utf-8");
MimeBodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.attachFile("/path-to/attach.txt");
MimeBodyPart fileBodyPart2 = new MimeBodyPart();
fileBodyPart2.attachFile("/path-to/attach2.txt");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(fileBodyPart);
//add another body part to supply another attachment
multipart.addBodyPart(fileBodyPart2);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
How It Works
Sending an e-mail message that contains HTML content is basically the same as send-
ing an e-mail with standard text—the only difference is the content type. When you're
setting the content on the message body part of the e-mail, you set the content to
text / html to have the content treated as HTML. There are various ways to construct
the HTML content, including using links, photos, or any other valid HTML markup. In
this example, a few basic HTML tags have been embedded into a string.
Although the example code may not be very useful in real-life systems, it is easy to
generate dynamic HTML content for inclusion within an e-mail. At its most basic
form, dynamically generated HTML can be strings of text that are concatenated to for-
mulate the HTML.
Search WWH ::




Custom Search