Send a multipart SMTP email message with java

In my last post I have shown how to send a simple SMTP email message.
This is good only for simple text messages, no HTML or attachments.

In this post I’ll show how to send multipart email messages, which can include HTML code, attachments and inner images.

First, take a look at this example from the last post.

MimeMessage message = new MimeMessage(session); 
message.setFrom(new InternetAddress(from)); 
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
message.setSubject(subject); 
message.setText(body); 
Transport.send(message); 

Now lets enhance that to add other features.


HTML Email

MimeMessage message = new MimeMessage(session); 
message.setFrom(new InternetAddress(from)); 
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
message.setSubject(subject); 
//message.setText(body); 
Multipart multipart = new MimeMultipart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<H1>This is HTML</H1>","text/html");
htmlPart.setDisposition(BodyPart.INLINE);
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
Transport.send(message); 



We still need to create a MimeMessage object, but we also need to create a Multipart object.

The Multipart object contains parts of the message which can be of different types.
The inner types are objects from type BodyPart , here we use MimeBodyPart with text/html format.

Notice also that instead of using “message.setText();” we use “message.setContent(Multipart);”.



Email with attachments

MimeMessage message = new MimeMessage(session); 
message.setFrom(new InternetAddress(from)); 
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
message.setSubject(subject); 
//message.setText(body); 
Multipart multipart = new MimeMultipart();
BodyPart attach = new MimeBodyPart();
DataSource source = new FileDataSource(<attachment_path>);
attach.setDataHandler(new DataHandler(source));
attach.setFileName(<attachment_name>);
multipart.addBodyPart(attach);
message.setContent(multipart);
Transport.send(message); 

Sending an Email with a attachment is actually not that different.
Instead of using “MimeBodyPart.setContent()” we use “MimeBodyPart.setDataHandler()” and “MimeBodyPart.setFileName()”.




Email with inline images

Sending an Email with inner images is kind of combination between the two, because you need to add the image as an attachment and you also need to reference it inside an HTML body part.

MimeMessage message = new MimeMessage(session); 
message.setFrom(new InternetAddress(from)); 
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
message.setSubject(subject); 

// Adding the HTML part
Multipart multipart = new MimeMultipart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("This is the image <IMG src=\"cid:image_example.gif\">","text/html"); 
htmlPart.setDisposition(BodyPart.INLINE);
multipart.addBodyPart(htmlPart);

// Adding the Image part
BodyPart attach = new MimeBodyPart();
DataSource source = new FileDataSource("example.gif");
attach.setDataHandler(new DataHandler(source));
attach.setFileName(fileName);
attach.setHeader("Content-ID","<image_example.gif>");
multipart.addBodyPart(attach);

message.setContent(multipart);
Transport.send(message); 

Notice that adding an inline image means you have to add the image file as an attachment.
But you also have to add the “Content-ID” parameter to the BodyPart header.
Beside that you also have to reffer to that image from inside the HTML code.

You will also need the mail.jar that can be found here.

Download:
downloadHtmlMail.java

downloadMailwithAttach.java

2 thoughts on “Send a multipart SMTP email message with java

  1. Thanks a lot for the above example. I have to display body content with attachment and tried that but in vain. Both the body parts were coming as an attachment. I went thru many many forums but nothing helped. Finally i came across your example and the code
    htmlPart.setDisposition(BodyPart.INLINE);
    message.setContent(multipart);
    made the magic.
    Thank you once again for this post.
    Thanks
    Sathya

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>