Skip to content
Advertisement

how to send a html email with attached file using JavaMail

The following Java code is used to attach a file to a html email and send it. I want to send attachment with this html email. Any suggestions would be appreciated.

JavaScript

This brings me just only the attachment . But i want to send html email with this attachment .

Advertisement

Answer

Creating a mail with an HTML body and an attachment, actually means creating a mail whose content is a “multipart entity”, that contains two parts, one of them being the HTML content, et the second being the attached file.

This does not correspond to your current code:

JavaScript

At this point, your email’s content is a multipart that has only 1 part, which is your attachment.

So, to reach your expected result, you should proceed differently :

  1. Create a multipart (as you did)
  2. Create a part, that has your file attachment as a content (as you did)
  3. Add this first part to the multipart (as you did)
  4. Create a second MimeBodyPart
  5. Add your html content to that second part
  6. Add this second part to your multipart
  7. Set your email’s content to be the multipart (as you did)

Which roughly translates to :

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement