Helper class for populating a {@link javax.mail.internet.MimeMessage}.
Mirrors the simple setters of {@link org.springframework.mail.SimpleMailMessage}, directly applying the values to the underlying MimeMessage. Allows for defining a character encoding for the entire message, automatically applied by all methods of this helper class.
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 {@link MimeMessagePreparator} implementations or{@link 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 {@link 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 {@link MimeMailMessage} (which implements the common{@link org.springframework.mail.MailMessage} interface, just like{@link org.springframework.mail.SimpleMailMessage}) on top of this helper, in order 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.
@author Juergen Hoeller
@since 19.01.2004
@see #setText(String,boolean)
@see #setText(String,String)
@see #addInline(String,org.springframework.core.io.Resource)
@see #addAttachment(String,org.springframework.core.io.InputStreamSource)
@see #MULTIPART_MODE_MIXED_RELATED
@see #MULTIPART_MODE_RELATED
@see #getMimeMessage()
@see JavaMailSender