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);
Array ( ) 2 Responses to “How to send HTML mail using JavaMail”
Leave a Reply
You must be logged in to post a comment.
September 16th, 2010 at 6:19 am
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
September 16th, 2010 at 9:29 am
How to attach an image is covered in here. Let me know if you have any questions.