Send HTML mail with images (Javamail)Tag(s): Networking
It's easy to send HTML mail with JavaMail. Simply set the content type to "text/html".
import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; class SimpleMail { public static void main(String[] args) throws Exception{ System.out.println("Sending mail..."); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.mymailserver.com"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); Session mailSession = Session.getDefaultInstance(props, null); mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail with images"); message.setFrom(new InternetAddress("me@sender.com")); message.setContent("<h1>Hello world</h1>", "text/html"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("you@receiver.com")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }
import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; class SimpleMail1 { public static void main(String[] args) throws Exception{ System.out.println("Sending mail..."); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.mymailserver.com"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); Session mailSession = Session.getDefaultInstance(props, null); mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail with images"); message.setFrom(new InternetAddress("me@sender.com")); message.setContent ("<h1>This is a test</h1>" + "<img src=\"http://www.rgagnon.com/images/jht.gif\">", "text/html"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("you@receiver.com")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }
An alternative to placing absolute URLs to images in your HTML is to include the images as attachments to the email. The HTML can reference the image in an attachment by using the protocol prefix cid: plus the content-id of the attachment.
import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.util.Properties; class SimpleMail2 { public static void main(String[] args) throws Exception{ System.out.println("Sending mail..."); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.mymailserver.com"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); Session mailSession = Session.getDefaultInstance(props, null); mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail with images"); message.setFrom(new InternetAddress("me@sender.com")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("you@receiver.com")); // // This HTML mail have to 2 part, the BODY and the embedded image // MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><img src=\"cid:image\">"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource ("C:\\images\\jht.gif"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID","<image>"); // add it multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }