Tuesday, 21 August 2012

How to use JavaMail to Send EMail with Embedded Images

A lot of us use email clients that are setup to not display external images by default.  We do this so that those senders of spam don't know that we have read the email.  The downside is that a lot of our incoming email looks rubbish.

I assume that most of the email that gets through the spam filter is not trying to track me, and that the use of external images is simply a lack of understanding of how to use embedded images.

Here is my example of using JavaMail to send email with embedded inline images:
// HTML we want to send.
// Note the "cid:" that precedes the image reference.
String html =
        "<html>" +
        "  <head>" +
        "    <style type='text/css'>" +
        "      body {background-color:blue}" +
        "      p {color:white}" +
        "    </style>" +
        "  </head>" +
        "  <body>" +
        "    <p><img src='cid:icon'></p>" +
        "    <p>Dear Bill</p>\n" +
        "    <p>This is a test mail.</p>" +
        "  </body>" +

// Create Email content
// Use related (rather than mixed) mime subtype,
// otherwise Thunderbird will not display images inline.
Multipart multipart = new MimeMultipart("related");

// Create the HTML message part and add it to the email content.
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(html, "text/html");

// Read the image from a file and add it to the email content.
// Note the "<>" added around the content ID used in the HTML above.
// Setting Content-Type does not seem to be required.
// I guess it is determined from the file type
MimeBodyPart iconBodyPart = new MimeBodyPart();
DataSource iconDataSource = new FileDataSource(new File("./icon.jpg"));
iconBodyPart.setDataHandler(new DataHandler(iconDataSource));
iconBodyPart.addHeader("Content-Type", "image/jpeg");

// Create the connection to the SMTP server to send the email.
final String smtpAccountUsername = "myUsername";
final String smtpAccountPassword = "myPassword";
final String smtpServer = "smtp.mymailserver.com";
final String smtpServerPort = "587";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", smtpServerPort);

Session session = Session.getInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(
                    smtpAccountUsername, smtpAccountPassword);

// Create email, set the content and send.
MimeMessage mail = new MimeMessage(session);
mail.setRecipients(RecipientType.TO, InternetAddress.parse("bill@example.com"));
mail.setSubject("Mail Subject Line");


No comments:

Post a Comment