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("mail.smtp.host", 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));
message.setSubject(subject);

// 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

mp.addBodyPart(htmlPart);

message.setContent(mp);

// Send the message

Transport.send(message);

written by objects \\ tags: , ,


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.

    Thanks,
    Ajay

  2. objects Says:

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

Leave a Reply