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
    MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage)
    Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content, i.e.
    MimeMessageHelper(jakarta.mail.internet.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(jakarta.mail.internet.MimeMessage mimeMessage, boolean multipart, @Nullable String encoding)
    Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.
    MimeMessageHelper(jakarta.mail.internet.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(jakarta.mail.internet.MimeMessage mimeMessage, int multipartMode, @Nullable String encoding)
    Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.
    MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage, @Nullable 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, jakarta.activation.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, InputStreamSource inputStreamSource)
    Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource.
    void
    addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType)
    Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource.
    void
    addBcc(jakarta.mail.internet.InternetAddress bcc)
     
    void
     
    void
    addBcc(String bcc, String personal)
     
    void
    addCc(jakarta.mail.internet.InternetAddress cc)
     
    void
     
    void
    addCc(String cc, String personal)
     
    void
    addInline(String contentId, jakarta.activation.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, @Nullable String inlineFilename, jakarta.activation.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, 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, 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, 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, Resource resource)
    Add an inline element to the MimeMessage, taking the content from a org.springframework.core.io.Resource.
    void
    addTo(jakarta.mail.internet.InternetAddress to)
     
    void
     
    void
    addTo(String to, String personal)
     
    protected jakarta.activation.DataSource
    createDataSource(InputStreamSource inputStreamSource, String contentType, String name)
    Create an Activation Framework DataSource for the given InputStreamSource.
    protected void
    createMimeMultiparts(jakarta.mail.internet.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 @Nullable String
    getDefaultEncoding(jakarta.mail.internet.MimeMessage mimeMessage)
    Determine the default encoding for the given MimeMessage.
    protected jakarta.activation.FileTypeMap
    getDefaultFileTypeMap(jakarta.mail.internet.MimeMessage mimeMessage)
    Determine the default Java Activation FileTypeMap for the given MimeMessage.
    Return the specific character encoding used for this message, if any.
    jakarta.activation.FileTypeMap
    Return the FileTypeMap used by this MimeMessageHelper.
    final jakarta.mail.internet.MimeMessage
    Return the underlying MimeMessage object.
    final jakarta.mail.internet.MimeMultipart
    Return the underlying MIME "multipart/related" object, if any.
    final jakarta.mail.internet.MimeMultipart
    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
    setBcc(jakarta.mail.internet.InternetAddress bcc)
     
    void
    setBcc(jakarta.mail.internet.InternetAddress[] bcc)
     
    void
     
    void
    setBcc(String[] bcc)
     
    void
    setCc(jakarta.mail.internet.InternetAddress cc)
     
    void
    setCc(jakarta.mail.internet.InternetAddress[] cc)
     
    void
     
    void
    setCc(String[] cc)
     
    void
    setEncodeFilenames(boolean encodeFilenames)
    Set whether to encode attachment filenames passed to this helper's #addAttachment methods.
    void
    setFileTypeMap(@Nullable jakarta.activation.FileTypeMap fileTypeMap)
    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
    setFrom(jakarta.mail.internet.InternetAddress from)
     
    void
     
    void
    setFrom(String from, String personal)
     
    protected final void
    setMimeMultiparts(@Nullable jakarta.mail.internet.MimeMultipart root, @Nullable jakarta.mail.internet.MimeMultipart main)
    Set the given MimeMultipart objects for use by this MimeMessageHelper.
    void
    setPriority(int priority)
    Set the priority ("X-Priority" header) of the message.
    void
    setReplyTo(jakarta.mail.internet.InternetAddress replyTo)
     
    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
    setTo(jakarta.mail.internet.InternetAddress to)
     
    void
    setTo(jakarta.mail.internet.InternetAddress[] to)
     
    void
     
    void
    setTo(String[] to)
     
    void
    setValidateAddresses(boolean validateAddresses)
    Set whether to validate all addresses which get passed to this helper.
    protected void
    validateAddress(jakarta.mail.internet.InternetAddress address)
    Validate the given mail address.
    protected void
    validateAddresses(jakarta.mail.internet.InternetAddress[] addresses)
    Validate all given mail addresses.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MULTIPART_MODE_NO

      public static final int MULTIPART_MODE_NO
      Constant indicating a non-multipart message.
      See Also:
    • MULTIPART_MODE_MIXED

      public static final int MULTIPART_MODE_MIXED
      Constant indicating a multipart message with a single root multipart element of type "mixed". Texts, inline elements and attachements will all get added to that root element.

      This was Spring 1.0's default behavior. It is known to work properly on Outlook. However, other mail clients tend to misinterpret inline elements as attachments and/or show attachments inline as well.

      See Also:
  • Constructor Details

    • MimeMessageHelper

      public MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage)
      Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content, i.e. no alternative texts and no inline elements or attachments).

      The character encoding for the message will be taken from the passed-in MimeMessage object, if carried there. Else, JavaMail's default encoding will be used.

      Parameters:
      mimeMessage - the mime message to work on
      See Also:
    • MimeMessageHelper

      public MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage, @Nullable String encoding)
      Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content, i.e. no alternative texts and no inline elements or attachments).
      Parameters:
      mimeMessage - the mime message to work on
      encoding - the character encoding to use for the message
      See Also:
    • MimeMessageHelper

      public MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage, boolean multipart) throws jakarta.mail.MessagingException
      Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.

      Consider using the MimeMessageHelper constructor that takes a multipartMode argument to choose a specific multipart mode other than MULTIPART_MODE_MIXED_RELATED.

      The character encoding for the message will be taken from the passed-in MimeMessage object, if carried there. Else, JavaMail's default encoding will be used.

      Parameters:
      mimeMessage - the mime message to work on
      multipart - whether to create a multipart message that supports alternative texts, inline elements and attachments (corresponds to MULTIPART_MODE_MIXED_RELATED)
      Throws:
      jakarta.mail.MessagingException - if multipart creation failed
      See Also:
    • MimeMessageHelper

      public MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage, boolean multipart, @Nullable String encoding) throws jakarta.mail.MessagingException
      Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.

      Consider using the MimeMessageHelper constructor that takes a multipartMode argument to choose a specific multipart mode other than MULTIPART_MODE_MIXED_RELATED.

      Parameters:
      mimeMessage - the mime message to work on
      multipart - whether to create a multipart message that supports alternative texts, inline elements and attachments (corresponds to MULTIPART_MODE_MIXED_RELATED)
      encoding - the character encoding to use for the message
      Throws:
      jakarta.mail.MessagingException - if multipart creation failed
      See Also:
    • MimeMessageHelper

      public MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage, int multipartMode) throws jakarta.mail.MessagingException
      Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.

      The character encoding for the message will be taken from the passed-in MimeMessage object, if carried there. Else, JavaMail's default encoding will be used.

      Parameters:
      mimeMessage - the mime message to work on
      multipartMode - which kind of multipart message to create (MIXED, RELATED, MIXED_RELATED, or NO)
      Throws:
      jakarta.mail.MessagingException - if multipart creation failed
      See Also:
    • MimeMessageHelper

      public MimeMessageHelper(jakarta.mail.internet.MimeMessage mimeMessage, int multipartMode, @Nullable String encoding) throws jakarta.mail.MessagingException
      Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.
      Parameters:
      mimeMessage - the mime message to work on
      multipartMode - which kind of multipart message to create (MIXED, RELATED, MIXED_RELATED, or NO)
      encoding - the character encoding to use for the message
      Throws:
      jakarta.mail.MessagingException - if multipart creation failed
      See Also:
  • Method Details

    • getMimeMessage

      public final jakarta.mail.internet.MimeMessage getMimeMessage()
      Return the underlying MimeMessage object.
    • createMimeMultiparts

      protected void createMimeMultiparts(jakarta.mail.internet.MimeMessage mimeMessage, int multipartMode) throws jakarta.mail.MessagingException
      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.

      Texts and inline elements can either be stored in the root element itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED) or in a nested element rather than the root element directly (MULTIPART_MODE_MIXED_RELATED).

      By default, the root MimeMultipart element will be of type "mixed" (MULTIPART_MODE_MIXED) or "related" (MULTIPART_MODE_RELATED). The main multipart element will either be added as nested element of type "related" (MULTIPART_MODE_MIXED_RELATED) or be identical to the root element itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED).

      Parameters:
      mimeMessage - the MimeMessage object to add the root MimeMultipart object to
      multipartMode - the multipart mode, as passed into the constructor (MIXED, RELATED, MIXED_RELATED, or NO)
      Throws:
      jakarta.mail.MessagingException - if multipart creation failed
      See Also:
    • setMimeMultiparts

      protected final void setMimeMultiparts(@Nullable jakarta.mail.internet.MimeMultipart root, @Nullable jakarta.mail.internet.MimeMultipart main)
      Set the given MimeMultipart objects for use by this MimeMessageHelper.
      Parameters:
      root - the root MimeMultipart object, which attachments will be added to; or null to indicate no multipart at all
      main - the main MimeMultipart object, which text(s) and inline elements will be added to (can be the same as the root multipart object, or an element nested underneath the root multipart element)
    • isMultipart

      public final boolean isMultipart()
      Return whether this helper is in multipart mode, i.e. whether it holds a multipart message.
      See Also:
    • getRootMimeMultipart

      public final jakarta.mail.internet.MimeMultipart getRootMimeMultipart() throws IllegalStateException
      Return the root MIME "multipart/mixed" object, if any. Can be used to manually add attachments.

      This will be the direct content of the MimeMessage, in case of a multipart mail.

      Throws:
      IllegalStateException - if this helper is not in multipart mode
      See Also:
    • getMimeMultipart

      public final jakarta.mail.internet.MimeMultipart getMimeMultipart() throws IllegalStateException
      Return the underlying MIME "multipart/related" object, if any. Can be used to manually add body parts, inline elements, etc.

      This will be nested within the root MimeMultipart, in case of a multipart mail.

      Throws:
      IllegalStateException - if this helper is not in multipart mode
      See Also:
    • getDefaultEncoding

      protected @Nullable String getDefaultEncoding(jakarta.mail.internet.MimeMessage mimeMessage)
      Determine the default encoding for the given MimeMessage.
      Parameters:
      mimeMessage - the passed-in MimeMessage
      Returns:
      the default encoding associated with the MimeMessage, or null if none found
    • getEncoding

      public @Nullable String getEncoding()
      Return the specific character encoding used for this message, if any.
    • getDefaultFileTypeMap

      protected jakarta.activation.FileTypeMap getDefaultFileTypeMap(jakarta.mail.internet.MimeMessage mimeMessage)
      Determine the default Java Activation FileTypeMap for the given MimeMessage.
      Parameters:
      mimeMessage - the passed-in MimeMessage
      Returns:
      the default FileTypeMap associated with the MimeMessage, or a default ConfigurableMimeFileTypeMap if none found for the message
      See Also:
    • setFileTypeMap

      public void setFileTypeMap(@Nullable jakarta.activation.FileTypeMap fileTypeMap)
      Set the Java Activation Framework FileTypeMap to use for determining the content type of inline content and attachments that get added to the message.

      The default is the FileTypeMap that the underlying MimeMessage carries, if any, or the Activation Framework's default FileTypeMap instance else.

      See Also:
    • getFileTypeMap

      public jakarta.activation.FileTypeMap getFileTypeMap()
      Return the FileTypeMap used by this MimeMessageHelper.
      See Also:
    • setEncodeFilenames

      public void setEncodeFilenames(boolean encodeFilenames)
      Set whether to encode attachment filenames passed to this helper's #addAttachment methods.

      The default is false for standard MIME behavior; turn this to true for compatibility with older email clients. On a related note, check out JavaMail's mail.mime.encodefilename system property.

      NOTE: The default changed to false in 5.3, in favor of JavaMail's standard mail.mime.encodefilename system property.

      Since:
      5.2.9
      See Also:
    • isEncodeFilenames

      public boolean isEncodeFilenames()
      Return whether to encode attachment filenames passed to this helper's #addAttachment methods.
      Since:
      5.2.9
      See Also:
    • setValidateAddresses

      public void setValidateAddresses(boolean validateAddresses)
      Set whether to validate all addresses which get passed to this helper.

      The default is false.

      See Also:
    • isValidateAddresses

      public boolean isValidateAddresses()
      Return whether this helper will validate all addresses passed to it.
      See Also:
    • validateAddress

      protected void validateAddress(jakarta.mail.internet.InternetAddress address) throws jakarta.mail.internet.AddressException
      Validate the given mail address. Called by all of MimeMessageHelper's address setters and adders.

      The default implementation invokes InternetAddress.validate(), provided that address validation is activated for the helper instance.

      Parameters:
      address - the address to validate
      Throws:
      jakarta.mail.internet.AddressException - if validation failed
      See Also:
    • validateAddresses

      protected void validateAddresses(jakarta.mail.internet.InternetAddress[] addresses) throws jakarta.mail.internet.AddressException
      Validate all given mail addresses.

      The default implementation simply delegates to validateAddress(InternetAddress) for each address.

      Parameters:
      addresses - the addresses to validate
      Throws:
      jakarta.mail.internet.AddressException - if validation failed
      See Also:
    • setFrom

      public void setFrom(jakarta.mail.internet.InternetAddress from) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setFrom

      public void setFrom(String from) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setFrom

      public void setFrom(String from, String personal) throws jakarta.mail.MessagingException, UnsupportedEncodingException
      Throws:
      jakarta.mail.MessagingException
      UnsupportedEncodingException
    • setReplyTo

      public void setReplyTo(jakarta.mail.internet.InternetAddress replyTo) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setReplyTo

      public void setReplyTo(String replyTo) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setReplyTo

      public void setReplyTo(String replyTo, String personal) throws jakarta.mail.MessagingException, UnsupportedEncodingException
      Throws:
      jakarta.mail.MessagingException
      UnsupportedEncodingException
    • setTo

      public void setTo(jakarta.mail.internet.InternetAddress to) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setTo

      public void setTo(jakarta.mail.internet.InternetAddress[] to) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setTo

      public void setTo(String to) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setTo

      public void setTo(String[] to) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addTo

      public void addTo(jakarta.mail.internet.InternetAddress to) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addTo

      public void addTo(String to) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addTo

      public void addTo(String to, String personal) throws jakarta.mail.MessagingException, UnsupportedEncodingException
      Throws:
      jakarta.mail.MessagingException
      UnsupportedEncodingException
    • setCc

      public void setCc(jakarta.mail.internet.InternetAddress cc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setCc

      public void setCc(jakarta.mail.internet.InternetAddress[] cc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setCc

      public void setCc(String cc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setCc

      public void setCc(String[] cc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addCc

      public void addCc(jakarta.mail.internet.InternetAddress cc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addCc

      public void addCc(String cc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addCc

      public void addCc(String cc, String personal) throws jakarta.mail.MessagingException, UnsupportedEncodingException
      Throws:
      jakarta.mail.MessagingException
      UnsupportedEncodingException
    • setBcc

      public void setBcc(jakarta.mail.internet.InternetAddress bcc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setBcc

      public void setBcc(jakarta.mail.internet.InternetAddress[] bcc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setBcc

      public void setBcc(String bcc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • setBcc

      public void setBcc(String[] bcc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addBcc

      public void addBcc(jakarta.mail.internet.InternetAddress bcc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addBcc

      public void addBcc(String bcc) throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • addBcc

      public void addBcc(String bcc, String personal) throws jakarta.mail.MessagingException, UnsupportedEncodingException
      Throws:
      jakarta.mail.MessagingException
      UnsupportedEncodingException
    • setPriority

      public void setPriority(int priority) throws jakarta.mail.MessagingException
      Set the priority ("X-Priority" header) of the message.
      Parameters:
      priority - the priority value; typically between 1 (highest) and 5 (lowest)
      Throws:
      jakarta.mail.MessagingException - in case of errors
    • setSentDate

      public void setSentDate(Date sentDate) throws jakarta.mail.MessagingException
      Set the sent-date of the message.
      Parameters:
      sentDate - the date to set (never null)
      Throws:
      jakarta.mail.MessagingException - in case of errors
    • setSubject

      public void setSubject(String subject) throws jakarta.mail.MessagingException
      Set the subject of the message, using the correct encoding.
      Parameters:
      subject - the subject text
      Throws:
      jakarta.mail.MessagingException - in case of errors
    • setText

      public void setText(String text) throws jakarta.mail.MessagingException
      Set the given text directly as content in non-multipart mode or as default body part in multipart mode. Always applies the default content type "text/plain".

      NOTE: Invoke addInline(String, DataSource) after setText; else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      text - the text for the message
      Throws:
      jakarta.mail.MessagingException - in case of errors
    • setText

      public void setText(String text, boolean html) throws jakarta.mail.MessagingException
      Set the given text directly as content in non-multipart mode or as default body part in multipart mode. The "html" flag determines the content type to apply.

      NOTE: Invoke addInline(String, DataSource) after setText; else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      text - the text for the message
      html - whether to apply content type "text/html" for an HTML mail, using default content type ("text/plain") else
      Throws:
      jakarta.mail.MessagingException - in case of errors
    • setText

      public void setText(String plainText, String htmlText) throws jakarta.mail.MessagingException
      Set the given plain text and HTML text as alternatives, offering both options to the email client. Requires multipart mode.

      NOTE: Invoke addInline(String, DataSource) after setText; else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      plainText - the plain text for the message
      htmlText - the HTML text for the message
      Throws:
      jakarta.mail.MessagingException - in case of errors
    • addInline

      public void addInline(String contentId, jakarta.activation.DataSource dataSource) throws jakarta.mail.MessagingException
      Add an inline element to the MimeMessage, taking the content from a jakarta.activation.DataSource.

      Note that the InputStream returned by the DataSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      NOTE: Invoke addInline after setText(String); else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      contentId - the content ID to use. Will end up as "Content-ID" header in the body part, surrounded by angle brackets: for example, "myId" → "<myId>". Can be referenced in HTML source via src="cid:myId" expressions.
      dataSource - the jakarta.activation.DataSource to take the content from, determining the InputStream and the content type
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • addInline

      public void addInline(String contentId, @Nullable String inlineFilename, jakarta.activation.DataSource dataSource) throws jakarta.mail.MessagingException
      Add an inline element to the MimeMessage, taking the content from a jakarta.activation.DataSource and assigning the provided inlineFileName to the element.

      Note that the InputStream returned by the DataSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      NOTE: Invoke addInline after setText(String); else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      contentId - the content ID to use. Will end up as "Content-ID" header in the body part, surrounded by angle brackets: for example, "myId" → "<myId>". Can be referenced in HTML source via src="cid:myId" expressions.
      inlineFilename - the fileName to use for the inline element's part
      dataSource - the jakarta.activation.DataSource to take the content from, determining the InputStream and the content type
      Throws:
      jakarta.mail.MessagingException - in case of errors
      Since:
      6.2
      See Also:
    • addInline

      public void addInline(String contentId, File file) throws jakarta.mail.MessagingException
      Add an inline element to the MimeMessage, taking the content from a java.io.File.

      The content type will be determined by the name of the given content file. Do not use this for temporary files with arbitrary filenames (possibly ending in ".tmp" or the like)!

      NOTE: Invoke addInline after setText(String); else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      contentId - the content ID to use. Will end up as "Content-ID" header in the body part, surrounded by angle brackets: for example, "myId" → "<myId>". Can be referenced in HTML source via src="cid:myId" expressions.
      file - the File resource to take the content from
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • addInline

      public void addInline(String contentId, Resource resource) throws jakarta.mail.MessagingException
      Add an inline element to the MimeMessage, taking the content from a org.springframework.core.io.Resource.

      The content type will be determined by the name of the given content file. Do not use this for temporary files with arbitrary filenames (possibly ending in ".tmp" or the like)!

      Note that the InputStream returned by the Resource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      NOTE: Invoke addInline after setText(String); else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      contentId - the content ID to use. Will end up as "Content-ID" header in the body part, surrounded by angle brackets: for example, "myId" → "<myId>". Can be referenced in HTML source via src="cid:myId" expressions.
      resource - the resource to take the content from
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • addInline

      public void addInline(String contentId, InputStreamSource inputStreamSource, String contentType) throws jakarta.mail.MessagingException
      Add an inline element to the MimeMessage, taking the content from an org.springframework.core.InputStreamResource, and specifying the content type explicitly.

      You can determine the content type for any given filename via a Java Activation Framework's FileTypeMap, for example the one held by this helper.

      Note that the InputStream returned by the InputStreamSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      NOTE: Invoke addInline after setText; else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      contentId - the content ID to use. Will end up as "Content-ID" header in the body part, surrounded by angle brackets: for example, "myId" → "<myId>". Can be referenced in HTML source via src="cid:myId" expressions.
      inputStreamSource - the resource to take the content from
      contentType - the content type to use for the element
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • addInline

      public void addInline(String contentId, String inlineFilename, InputStreamSource inputStreamSource) throws jakarta.mail.MessagingException
      Add an inline element to the MimeMessage, taking the content from an org.springframework.core.InputStreamResource, and specifying the inline fileName explicitly.

      The content type will be determined by the name of the given content file. Do not use this for temporary files with arbitrary filenames (possibly ending in ".tmp" or the like)!

      Note that the InputStream returned by the InputStreamSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      NOTE: Invoke addInline after setText; else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      contentId - the content ID to use. Will end up as "Content-ID" header in the body part, surrounded by angle brackets: for example, "myId" → "<myId>". Can be referenced in HTML source via src="cid:myId" expressions.
      inlineFilename - the file name to use for the inline element
      inputStreamSource - the resource to take the content from
      Throws:
      jakarta.mail.MessagingException - in case of errors
      Since:
      6.2
      See Also:
    • addInline

      public void addInline(String contentId, String inlineFilename, InputStreamSource inputStreamSource, String contentType) throws jakarta.mail.MessagingException
      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.

      You can determine the content type for any given filename via a Java Activation Framework's FileTypeMap, for example the one held by this helper.

      Note that the InputStream returned by the InputStreamSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      NOTE: Invoke addInline after setText; else, mail readers might not be able to resolve inline references correctly.

      Parameters:
      contentId - the content ID to use. Will end up as "Content-ID" header in the body part, surrounded by angle brackets: for example, "myId" → "<myId>". Can be referenced in HTML source via src="cid:myId" expressions.
      inlineFilename - the fileName to use for the inline element's part
      inputStreamSource - the resource to take the content from
      contentType - the content type to use for the element
      Throws:
      jakarta.mail.MessagingException - in case of errors
      Since:
      6.2
      See Also:
    • addAttachment

      public void addAttachment(String attachmentFilename, jakarta.activation.DataSource dataSource) throws jakarta.mail.MessagingException
      Add an attachment to the MimeMessage, taking the content from a jakarta.activation.DataSource.

      Note that the InputStream returned by the DataSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      Parameters:
      attachmentFilename - the name of the attachment as it will appear in the mail (the content type will be determined by this)
      dataSource - the jakarta.activation.DataSource to take the content from, determining the InputStream and the content type
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • addAttachment

      public void addAttachment(String attachmentFilename, File file) throws jakarta.mail.MessagingException
      Add an attachment to the MimeMessage, taking the content from a java.io.File.

      The content type will be determined by the name of the given content file. Do not use this for temporary files with arbitrary filenames (possibly ending in ".tmp" or the like)!

      Parameters:
      attachmentFilename - the name of the attachment as it will appear in the mail
      file - the File resource to take the content from
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • addAttachment

      public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) throws jakarta.mail.MessagingException
      Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource.

      The content type will be determined by the given filename for the attachment. Thus, any content source will be fine, including temporary files with arbitrary filenames.

      Note that the InputStream returned by the InputStreamSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      Parameters:
      attachmentFilename - the name of the attachment as it will appear in the mail
      inputStreamSource - the resource to take the content from (all of Spring's Resource implementations can be passed in here)
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • addAttachment

      public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType) throws jakarta.mail.MessagingException
      Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource.

      Note that the InputStream returned by the InputStreamSource implementation needs to be a fresh one on each call, as JavaMail will invoke getInputStream() multiple times.

      Parameters:
      attachmentFilename - the name of the attachment as it will appear in the mail
      inputStreamSource - the resource to take the content from (all of Spring's Resource implementations can be passed in here)
      contentType - the content type to use for the element
      Throws:
      jakarta.mail.MessagingException - in case of errors
      See Also:
    • createDataSource

      protected jakarta.activation.DataSource createDataSource(InputStreamSource inputStreamSource, String contentType, String name)
      Create an Activation Framework DataSource for the given InputStreamSource.
      Parameters:
      inputStreamSource - the InputStreamSource (typically a Spring Resource)
      contentType - the content type
      name - the name of the DataSource
      Returns:
      the Activation Framework DataSource