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.
Now lets enhance that to add other features.
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);”.
Sending an Email with a attachment is actually not that different.
Instead of using “MimeBodyPart.setContent()” we use “MimeBodyPart.setDataHandler()” and “MimeBodyPart.setFileName()”.
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.
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.
This entry was posted in Java_Mail and tagged Java, SMTP by Avi. Bookmark the permalink.
your code was very helpful, though i tried searching in many forums. Thanks.