Class MimeMessageParser


  • public class MimeMessageParser
    extends java.lang.Object
    Parses a MimeMessage and stores the individual parts such a plain text, HTML text and attachments.
    Since:
    1.3
    • Constructor Summary

      Constructors 
      Constructor Description
      MimeMessageParser​(javax.mail.internet.MimeMessage message)
      Constructs an instance with the MimeMessage to be extracted.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected javax.activation.DataSource createDataSource​(javax.mail.Multipart parent, javax.mail.internet.MimePart part)
      Parses the MimePart to create a DataSource.
      javax.activation.DataSource findAttachmentByCid​(java.lang.String cid)
      Find an attachment using its content-id.
      javax.activation.DataSource findAttachmentByName​(java.lang.String name)
      Find an attachment using its name.
      java.util.List<javax.activation.DataSource> getAttachmentList()  
      java.util.List<javax.mail.Address> getBcc()  
      java.util.List<javax.mail.Address> getCc()  
      java.util.Collection<java.lang.String> getContentIds()
      Returns a collection of all content-ids in the parsed message.
      protected java.lang.String getDataSourceName​(javax.mail.Part part, javax.activation.DataSource dataSource)
      Determines the name of the data source if it is not already set.
      java.lang.String getFrom()  
      java.lang.String getHtmlContent()  
      javax.mail.internet.MimeMessage getMimeMessage()  
      java.lang.String getPlainContent()  
      java.lang.String getReplyTo()  
      java.lang.String getSubject()  
      java.util.List<javax.mail.Address> getTo()  
      boolean hasAttachments()  
      boolean hasHtmlContent()  
      boolean hasPlainContent()  
      boolean isMultipart()  
      MimeMessageParser parse()
      Does the actual extraction.
      protected void parse​(javax.mail.Multipart parent, javax.mail.internet.MimePart part)
      Extracts the content of a MimeMessage recursively.
      • Methods inherited from class java.lang.Object

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

      • MimeMessageParser

        public MimeMessageParser​(javax.mail.internet.MimeMessage message)
        Constructs an instance with the MimeMessage to be extracted.
        Parameters:
        message - the message to parse
    • Method Detail

      • parse

        public MimeMessageParser parse()
                                throws java.lang.Exception
        Does the actual extraction.
        Returns:
        this instance
        Throws:
        java.lang.Exception - parsing the mime message failed
      • getTo

        public java.util.List<javax.mail.Address> getTo()
                                                 throws java.lang.Exception
        Returns:
        the 'to' recipients of the message
        Throws:
        java.lang.Exception - determining the recipients failed
      • getCc

        public java.util.List<javax.mail.Address> getCc()
                                                 throws java.lang.Exception
        Returns:
        the 'cc' recipients of the message
        Throws:
        java.lang.Exception - determining the recipients failed
      • getBcc

        public java.util.List<javax.mail.Address> getBcc()
                                                  throws java.lang.Exception
        Returns:
        the 'bcc' recipients of the message
        Throws:
        java.lang.Exception - determining the recipients failed
      • getFrom

        public java.lang.String getFrom()
                                 throws java.lang.Exception
        Returns:
        the 'from' field of the message
        Throws:
        java.lang.Exception - parsing the mime message failed
      • getReplyTo

        public java.lang.String getReplyTo()
                                    throws java.lang.Exception
        Returns:
        the 'replyTo' address of the email
        Throws:
        java.lang.Exception - parsing the mime message failed
      • getSubject

        public java.lang.String getSubject()
                                    throws java.lang.Exception
        Returns:
        the mail subject
        Throws:
        java.lang.Exception - parsing the mime message failed
      • parse

        protected void parse​(javax.mail.Multipart parent,
                             javax.mail.internet.MimePart part)
                      throws javax.mail.MessagingException,
                             java.io.IOException
        Extracts the content of a MimeMessage recursively.
        Parameters:
        parent - the parent multi-part
        part - the current MimePart
        Throws:
        javax.mail.MessagingException - parsing the MimeMessage failed
        java.io.IOException - parsing the MimeMessage failed
      • createDataSource

        protected javax.activation.DataSource createDataSource​(javax.mail.Multipart parent,
                                                               javax.mail.internet.MimePart part)
                                                        throws javax.mail.MessagingException,
                                                               java.io.IOException
        Parses the MimePart to create a DataSource.
        Parameters:
        parent - the parent multi-part
        part - the current part to be processed
        Returns:
        the DataSource
        Throws:
        javax.mail.MessagingException - creating the DataSource failed
        java.io.IOException - creating the DataSource failed
      • getMimeMessage

        public javax.mail.internet.MimeMessage getMimeMessage()
        Returns:
        Returns the mimeMessage.
      • isMultipart

        public boolean isMultipart()
        Returns:
        Returns the isMultiPart.
      • getPlainContent

        public java.lang.String getPlainContent()
        Returns:
        Returns the plainContent if any
      • getAttachmentList

        public java.util.List<javax.activation.DataSource> getAttachmentList()
        Returns:
        Returns the attachmentList.
      • getContentIds

        public java.util.Collection<java.lang.String> getContentIds()
        Returns a collection of all content-ids in the parsed message.

        The content-ids are stripped of any angle brackets, i.e. "part1" instead of "<part1>".

        Returns:
        the collection of content ids.
        Since:
        1.3.4
      • getHtmlContent

        public java.lang.String getHtmlContent()
        Returns:
        Returns the htmlContent if any
      • hasPlainContent

        public boolean hasPlainContent()
        Returns:
        true if a plain content is available
      • hasHtmlContent

        public boolean hasHtmlContent()
        Returns:
        true if HTML content is available
      • hasAttachments

        public boolean hasAttachments()
        Returns:
        true if attachments are available
      • findAttachmentByName

        public javax.activation.DataSource findAttachmentByName​(java.lang.String name)
        Find an attachment using its name.
        Parameters:
        name - the name of the attachment
        Returns:
        the corresponding datasource or null if nothing was found
      • findAttachmentByCid

        public javax.activation.DataSource findAttachmentByCid​(java.lang.String cid)
        Find an attachment using its content-id.

        The content-id must be stripped of any angle brackets, i.e. "part1" instead of "<part1>".

        Parameters:
        cid - the content-id of the attachment
        Returns:
        the corresponding datasource or null if nothing was found
        Since:
        1.3.4
      • getDataSourceName

        protected java.lang.String getDataSourceName​(javax.mail.Part part,
                                                     javax.activation.DataSource dataSource)
                                              throws javax.mail.MessagingException,
                                                     java.io.UnsupportedEncodingException
        Determines the name of the data source if it is not already set.
        Parameters:
        part - the mail part
        dataSource - the data source
        Returns:
        the name of the data source or null if no name can be determined
        Throws:
        javax.mail.MessagingException - accessing the part failed
        java.io.UnsupportedEncodingException - decoding the text failed