Class MimeMessageUtils


  • public class MimeMessageUtils
    extends java.lang.Object
    Copied and simplified from Spring MimeMessageHelper
    • Field Summary

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

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAttachment​(java.lang.String attachmentFilename, java.io.File file)
      Add an attachment to the MimeMessage, taking the content from a java.io.File.
      void addAttachment​(java.lang.String attachmentFilename, javax.activation.DataSource dataSource)
      Add an attachment to the MimeMessage, taking the content from a jakarta.activation.DataSource.
      protected void createMimeMultiparts​(javax.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.
      java.lang.String getEncoding()  
      javax.mail.internet.MimeMessage getMimeMessage()  
      javax.mail.internet.MimeMultipart getMimeMultipart()
      Return the underlying MIME "multipart/related" object, if any.
      javax.mail.internet.MimeMultipart getRootMimeMultipart()
      Return the root MIME "multipart/mixed" object, if any.
      boolean isEncodeFilenames()
      Return whether to encode attachment filenames passed to this helper's #addAttachment methods.
      boolean isMultipart()  
      void setBcc​(java.lang.String bcc)  
      void setBcc​(java.lang.String[] bcc)  
      void setBcc​(javax.mail.internet.InternetAddress bcc)  
      void setBcc​(javax.mail.internet.InternetAddress[] bcc)  
      void setCc​(java.lang.String cc)  
      void setCc​(java.lang.String[] cc)  
      void setCc​(javax.mail.internet.InternetAddress cc)  
      void setCc​(javax.mail.internet.InternetAddress[] cc)  
      void setEncodeFilenames​(boolean encodeFilenames)
      Set whether to encode attachment filenames passed to this helper's #addAttachment methods.
      void setFrom​(java.lang.String from)  
      void setFrom​(java.lang.String from, java.lang.String personal)  
      void setFrom​(javax.mail.internet.InternetAddress from)  
      protected void setMimeMultiparts​(javax.mail.internet.MimeMultipart root, javax.mail.internet.MimeMultipart main)
      Set the given MimeMultipart objects for use by this MimeMessageUtils.
      void setPriority​(int priority)
      Set the priority ("X-Priority" header) of the message.
      void setReplyTo​(java.lang.String replyTo)  
      void setReplyTo​(java.lang.String replyTo, java.lang.String personal)  
      void setReplyTo​(javax.mail.internet.InternetAddress replyTo)  
      void setSentDate​(java.util.Date sentDate)
      Set the sent-date of the message.
      void setSubject​(java.lang.String subject)
      Set the subject of the message, using the correct encoding.
      void setText​(java.lang.String text)
      Set the given text directly as content in non-multipart mode or as default body part in multipart mode.
      void setText​(java.lang.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​(java.lang.String plainText, java.lang.String htmlText)
      Set the given plain text and HTML text as alternatives, offering both options to the email client.
      void setTo​(java.lang.String to)  
      void setTo​(java.lang.String[] to)  
      void setTo​(javax.mail.internet.InternetAddress to)  
      void setTo​(javax.mail.internet.InternetAddress[] to)  
      • Methods inherited from class java.lang.Object

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

      • MULTIPART_MODE_NO

        public static final int MULTIPART_MODE_NO
        Constant indicating a non-multipart message.
        See Also:
        Constant Field Values
      • 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:
        Constant Field Values
      • MULTIPART_MODE_RELATED

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

        This was the default behavior from Spring 1.1 up to 1.2 final. This is the "Microsoft multipart mode", as natively sent by Outlook. It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and to a large degree also on Mac Mail (with an additional attachment listed for an inline element, despite the inline element also shown inline). Does not work properly on Lotus Notes (attachments won't be shown there).

        See Also:
        Constant Field Values
      • MULTIPART_MODE_MIXED_RELATED

        public static final int MULTIPART_MODE_MIXED_RELATED
        Constant indicating a multipart message with a root multipart element "mixed" plus a nested multipart element of type "related". Texts and inline elements will get added to the nested "related" element, while attachments will get added to the "mixed" root element.

        This is the default since Spring 1.2.1. This is arguably the most correct MIME structure, according to the MIME spec: It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and Lotus Notes. Does not work properly on Mac Mail. If you target Mac Mail or experience issues with specific mails on Outlook, consider using MULTIPART_MODE_RELATED instead.

        See Also:
        Constant Field Values
    • Constructor Detail

      • MimeMessageUtils

        public MimeMessageUtils​(javax.mail.internet.MimeMessage mimeMessage)
        Create a new MimeMessageUtils 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
      • MimeMessageUtils

        public MimeMessageUtils​(javax.mail.internet.MimeMessage mimeMessage,
                                java.lang.String encoding)
        Create a new MimeMessageUtils 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
      • MimeMessageUtils

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

        Consider using the MimeMessageUtils 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:
        javax.mail.MessagingException - if multipart creation failed
      • MimeMessageUtils

        public MimeMessageUtils​(javax.mail.internet.MimeMessage mimeMessage,
                                boolean multipart,
                                java.lang.String encoding)
                         throws javax.mail.MessagingException
        Create a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.

        Consider using the MimeMessageUtils 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:
        javax.mail.MessagingException - if multipart creation failed
      • MimeMessageUtils

        public MimeMessageUtils​(javax.mail.internet.MimeMessage mimeMessage,
                                int multipartMode)
                         throws javax.mail.MessagingException
        Create a new MimeMessageUtils 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:
        javax.mail.MessagingException - if multipart creation failed
      • MimeMessageUtils

        public MimeMessageUtils​(javax.mail.internet.MimeMessage mimeMessage,
                                int multipartMode,
                                java.lang.String encoding)
                         throws javax.mail.MessagingException
        Create a new MimeMessageUtils 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:
        javax.mail.MessagingException - if multipart creation failed
    • Method Detail

      • getMimeMessage

        public final javax.mail.internet.MimeMessage getMimeMessage()
      • createMimeMultiparts

        protected void createMimeMultiparts​(javax.mail.internet.MimeMessage mimeMessage,
                                            int multipartMode)
                                     throws javax.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:
        javax.mail.MessagingException - if multipart creation failed
      • setMimeMultiparts

        protected final void setMimeMultiparts​(@Nullable
                                               javax.mail.internet.MimeMultipart root,
                                               @Nullable
                                               javax.mail.internet.MimeMultipart main)
        Set the given MimeMultipart objects for use by this MimeMessageUtils.
        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()
      • getRootMimeMultipart

        public final javax.mail.internet.MimeMultipart getRootMimeMultipart()
                                                                     throws java.lang.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.

        Returns:
        the root MIME "multipart/mixed" object
        Throws:
        java.lang.IllegalStateException - if this helper is not in multipart mode
      • getMimeMultipart

        public final javax.mail.internet.MimeMultipart getMimeMultipart()
                                                                 throws java.lang.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.

        Returns:
        the root MIME "multipart/related" object
        Throws:
        java.lang.IllegalStateException - if this helper is not in multipart mode
      • getEncoding

        public java.lang.String getEncoding()
      • 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.

        Parameters:
        encodeFilenames - whether to encode attachment filenames
      • isEncodeFilenames

        public boolean isEncodeFilenames()
        Return whether to encode attachment filenames passed to this helper's #addAttachment methods.
      • setFrom

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

        public void setFrom​(java.lang.String from)
                     throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setFrom

        public void setFrom​(java.lang.String from,
                            java.lang.String personal)
                     throws javax.mail.MessagingException,
                            java.io.UnsupportedEncodingException
        Throws:
        javax.mail.MessagingException
        java.io.UnsupportedEncodingException
      • setReplyTo

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

        public void setReplyTo​(java.lang.String replyTo)
                        throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setReplyTo

        public void setReplyTo​(java.lang.String replyTo,
                               java.lang.String personal)
                        throws javax.mail.MessagingException,
                               java.io.UnsupportedEncodingException
        Throws:
        javax.mail.MessagingException
        java.io.UnsupportedEncodingException
      • setTo

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

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

        public void setTo​(java.lang.String to)
                   throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setTo

        public void setTo​(java.lang.String[] to)
                   throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setCc

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

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

        public void setCc​(java.lang.String cc)
                   throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setCc

        public void setCc​(java.lang.String[] cc)
                   throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setBcc

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

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

        public void setBcc​(java.lang.String bcc)
                    throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setBcc

        public void setBcc​(java.lang.String[] bcc)
                    throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • setPriority

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

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

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

        public void setText​(java.lang.String text)
                     throws javax.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". else, mail readers might not be able to resolve inline references correctly.
        Parameters:
        text - the text for the message
        Throws:
        javax.mail.MessagingException - in case of errors
      • setText

        public void setText​(java.lang.String text,
                            boolean html)
                     throws javax.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. 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:
        javax.mail.MessagingException - in case of errors
      • setText

        public void setText​(java.lang.String plainText,
                            java.lang.String htmlText)
                     throws javax.mail.MessagingException
        Set the given plain text and HTML text as alternatives, offering both options to the email client. Requires multipart mode. 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:
        javax.mail.MessagingException - in case of errors
      • addAttachment

        public void addAttachment​(java.lang.String attachmentFilename,
                                  javax.activation.DataSource dataSource)
                           throws javax.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:
        javax.mail.MessagingException - in case of errors
      • addAttachment

        public void addAttachment​(java.lang.String attachmentFilename,
                                  java.io.File file)
                           throws javax.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:
        javax.mail.MessagingException - in case of errors