Class MimeMessageHelper

java.lang.Object
org.springframework.mail.javamail.MimeMessageHelper

public class MimeMessageHelper extends Object
Helper class for populating a MimeMessage.

Mirrors the simple setters of 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 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("[email protected]");
     message.setTo("[email protected]");
     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 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.

Since:
19.01.2004
Author:
Juergen Hoeller, Sam Brannen
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Constant indicating a multipart message with a single root multipart element of type "mixed".
    static final int
    Constant indicating a multipart message with a root multipart element "mixed" plus a nested multipart element of type "related".
    static final int
    Constant indicating a non-multipart message.
    static final int
    Constant indicating a multipart message with a single root multipart element of type "related".
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content, i.e.
    MimeMessageHelper(MimeMessage mimeMessage, boolean multipart)
    Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.
    MimeMessageHelper(MimeMessage mimeMessage, boolean multipart, String encoding)
    Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.
    MimeMessageHelper(MimeMessage mimeMessage, int multipartMode)
    Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.
    MimeMessageHelper(MimeMessage mimeMessage, int multipartMode, String encoding)
    Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.
    MimeMessageHelper(MimeMessage mimeMessage, String encoding)
    Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content, i.e.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addAttachment(String attachmentFilename, DataSource dataSource)
    Add an attachment to the MimeMessage, taking the content from a jakarta.activation.DataSource.
    void
    addAttachment(String attachmentFilename, File file)
    Add an attachment to the MimeMessage, taking the content from a java.io.File.
    void
    addAttachment(String attachmentFilename, org.springframework.core.io.InputStreamSource inputStreamSource)
    Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource.
    void
    addAttachment(String attachmentFilename, org.springframework.core.io.InputStreamSource inputStreamSource, String contentType)
    Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource.
    void
     
    void
     
    void
    addBcc(String bcc, String personal)
     
    void
     
    void
     
    void
    addCc(String cc, String personal)
     
    void
    addInline(String contentId, DataSource dataSource)
    Add an inline element to the MimeMessage, taking the content from a jakarta.activation.DataSource.
    void
    addInline(String contentId, File file)
    Add an inline element to the MimeMessage, taking the content from a java.io.File.
    void
    addInline(String contentId, String inlineFilename, DataSource dataSource)
    Add an inline element to the MimeMessage, taking the content from a jakarta.activation.DataSource and assigning the provided inlineFileName to the element.
    void
    addInline(String contentId, String inlineFilename, org.springframework.core.io.InputStreamSource inputStreamSource)
    Add an inline element to the MimeMessage, taking the content from an org.springframework.core.InputStreamResource, and specifying the inline fileName explicitly.
    void
    addInline(String contentId, String inlineFilename, org.springframework.core.io.InputStreamSource inputStreamSource, String contentType)
    Add an inline element to the MimeMessage, taking the content from an org.springframework.core.InputStreamResource, and specifying the inline fileName and content type explicitly.
    void
    addInline(String contentId, org.springframework.core.io.InputStreamSource inputStreamSource, String contentType)
    Add an inline element to the MimeMessage, taking the content from an org.springframework.core.InputStreamResource, and specifying the content type explicitly.
    void
    addInline(String contentId, org.springframework.core.io.Resource resource)
    Add an inline element to the MimeMessage, taking the content from a org.springframework.core.io.Resource.
    void
     
    void
     
    void
    addTo(String to, String personal)
     
    protected DataSource
    createDataSource(org.springframework.core.io.InputStreamSource inputStreamSource, String contentType, String name)
    Create an Activation Framework DataSource for the given InputStreamSource.
    protected void
    createMimeMultiparts(MimeMessage mimeMessage, int multipartMode)
    Determine the MimeMultipart objects to use, which will be used to store attachments on the one hand and text(s) and inline elements on the other hand.
    protected String
    Determine the default encoding for the given MimeMessage.
    protected FileTypeMap
    Determine the default Java Activation FileTypeMap for the given MimeMessage.
    Return the specific character encoding used for this message, if any.
    Return the FileTypeMap used by this MimeMessageHelper.
    Return the underlying MimeMessage object.
    Return the underlying MIME "multipart/related" object, if any.
    Return the root MIME "multipart/mixed" object, if any.
    boolean
    Return whether to encode attachment filenames passed to this helper's #addAttachment methods.
    final boolean
    Return whether this helper is in multipart mode, i.e.
    boolean
    Return whether this helper will validate all addresses passed to it.
    void
     
    void
     
    void
     
    void
    setBcc(String[] bcc)
     
    void
     
    void
     
    void
     
    void
    setCc(String[] cc)
     
    void
    setEncodeFilenames(boolean encodeFilenames)
    Set whether to encode attachment filenames passed to this helper's #addAttachment methods.
    void
    Set the Java Activation Framework FileTypeMap to use for determining the content type of inline content and attachments that get added to the message.
    void
     
    void
     
    void
    setFrom(String from, String personal)
     
    protected final void
    Set the given MimeMultipart objects for use by this MimeMessageHelper.
    void
    setPriority(int priority)
    Set the priority ("X-Priority" header) of the message.
    void
     
    void
    setReplyTo(String replyTo)
     
    void
    setReplyTo(String replyTo, String personal)
     
    void
    setSentDate(Date sentDate)
    Set the sent-date of the message.
    void
    setSubject(String subject)
    Set the subject of the message, using the correct encoding.
    void
    Set the given text directly as content in non-multipart mode or as default body part in multipart mode.
    void
    setText(String text, boolean html)
    Set the given text directly as content in non-multipart mode or as default body part in multipart mode.
    void
    setText(String plainText, String htmlText)
    Set the given plain text and HTML text as alternatives, offering both options to the email client.
    void
     
    void
     
    void
     
    void
    setTo(String[] to)
     
    void
    setValidateAddresses(boolean validateAddresses)
    Set whether to validate all addresses which get passed to this helper.
    protected void
    Validate the given mail address.
    protected void
    Validate all given mail addresses.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait