Aug 12

Multipart emails can be used to send html content with JavaMail as shown in the following example.

// Create session
Properties sessionProperties = System.getProperties();
sessionProperties.put("", smtpServer);
Session session = Session.getDefaultInstance(sessionProperties, null);

// Create message

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
		to, false));

// Create Multipart to add content to 

Multipart mp = new MimeMultipart();

// Create a Part for the html content

String html = "<html><body><b>Test</b> email</body></html>";
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html");

// Add html part to your Multipart



// Send the message


written by objects \\ tags: , ,

Array ( ) 2 Responses to “How to send HTML mail using JavaMail”

  1. ajay Says:

    how do you attach the image into the HTML content ?
    I do not want to provide a URL within html that points to an image outsdie on the server.


  2. objects Says:

    How to attach an image is covered in here. Let me know if you have any questions.

Leave a Reply

You must be logged in to post a comment.