Helper class for easy population of a javax.mail.internet.MimeMessage.
Mirrors the simple setters of SimpleMailMessage, directly applying the values
to the underlying MimeMessage. Allows to define a character encoding for the
entire message, automatically applied by all methods of this helper.
Offers support for HTML text content, inline elements such as images, and typical
mail attachments. Also supports personal names that accompany mail addresses. Note that
advanced settings can still be applied directly to the underlying MimeMessage object!
Typically used in MimeMessagePreparator implementations or JavaMailSender
client code: simply instantiating it as a MimeMessage wrapper, invoking
setters on the wrapper, using the underlying MimeMessage for mail sending.
Also used internally by JavaMailSenderImpl.
Sample code for an HTML mail with an inline image and a PDF attachment:
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom("me@mail.com");
message.setTo("you@mail.com");
message.setSubject("my subject");
message.setText("my text <img src='cid:myLogo'>", true);
message.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));
}
});
Consider using MimeMailMessage (which implements the common MailMessage
interface, just like SimpleMailMessage) on top of a MimeMessageHelper,
to let message population code interact with a simple message or a MIME
message through a common interface.
Warning regarding multipart mails: Simple MIME messages that
just contain HTML text but no inline elements or attachments will work on
more or less any email client that is capable of HTML rendering. However,
inline elements and attachments are still a major compatibility issue
between email clients: It's virtually impossible to get inline elements
and attachments working across Microsoft Outlook, Lotus Notes and Mac Mail.
Consider choosing a specific multipart mode for your needs: The javadoc
on the MULTIPART_MODE constants contains more detailed information.
javax.mail.internet.MimeMessage.Mirrors the simple setters of SimpleMailMessage, directly applying the values to the underlying MimeMessage. Allows to define a character encoding for the entire message, automatically applied by all methods of this helper.
Offers support for HTML text content, inline elements such as images, and typical mail attachments. Also supports personal names that accompany mail addresses. Note that advanced settings can still be applied directly to the underlying MimeMessage object!
Typically used in MimeMessagePreparator implementations or JavaMailSender client code: simply instantiating it as a MimeMessage wrapper, invoking setters on the wrapper, using the underlying MimeMessage for mail sending. Also used internally by JavaMailSenderImpl.
Sample code for an HTML mail with an inline image and a PDF attachment:
mailSender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); message.setFrom("me@mail.com"); message.setTo("you@mail.com"); message.setSubject("my subject"); message.setText("my text <img src='cid:myLogo'>", true); message.addInline("myLogo", new ClassPathResource("img/mylogo.gif")); message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf")); } });Consider using MimeMailMessage (which implements the common MailMessage interface, just like SimpleMailMessage) on top of a MimeMessageHelper, to let message population code interact with a simple message or a MIME message through a common interface.Warning regarding multipart mails: Simple MIME messages that just contain HTML text but no inline elements or attachments will work on more or less any email client that is capable of HTML rendering. However, inline elements and attachments are still a major compatibility issue between email clients: It's virtually impossible to get inline elements and attachments working across Microsoft Outlook, Lotus Notes and Mac Mail. Consider choosing a specific multipart mode for your needs: The javadoc on the MULTIPART_MODE constants contains more detailed information.