Class MimeMessageParser

java.lang.Object
org.apache.commons.mail2.jakarta.util.MimeMessageParser

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

    • MimeMessageParser

      public MimeMessageParser(jakarta.mail.internet.MimeMessage mimeMessage)
      Constructs an instance with the MimeMessage to be extracted.
      Parameters:
      mimeMessage - the message to parse
  • Method Details

    • createDataSource

      protected jakarta.activation.DataSource createDataSource(jakarta.mail.Multipart parent, jakarta.mail.internet.MimePart part) throws jakarta.mail.MessagingException, 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:
      jakarta.mail.MessagingException - creating the DataSource failed
      IOException - error getting InputStream or unsupported encoding
    • findAttachmentByCid

      public jakarta.activation.DataSource findAttachmentByCid(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
    • findAttachmentByName

      public jakarta.activation.DataSource findAttachmentByName(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
    • getAttachmentList

      public List<jakarta.activation.DataSource> getAttachmentList()
      Gets the attachment list.
      Returns:
      Returns the attachment list.
    • getBcc

      public List<jakarta.mail.Address> getBcc() throws jakarta.mail.MessagingException
      Gets the BCC Address list.
      Returns:
      the 'BCC' recipients of the message
      Throws:
      jakarta.mail.MessagingException - determining the recipients failed
    • getCc

      public List<jakarta.mail.Address> getCc() throws jakarta.mail.MessagingException
      Gets the CC Address list.
      Returns:
      the 'CC' recipients of the message
      Throws:
      jakarta.mail.MessagingException - determining the recipients failed
    • 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
    • getDataSourceName

      protected String getDataSourceName(jakarta.mail.Part part, jakarta.activation.DataSource dataSource) throws jakarta.mail.MessagingException, 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:
      jakarta.mail.MessagingException - accessing the part failed
      UnsupportedEncodingException - decoding the text failed
    • getFrom

      public String getFrom() throws jakarta.mail.MessagingException
      Gets the FROM field.
      Returns:
      the FROM field of the message
      Throws:
      jakarta.mail.MessagingException - parsing the mime message failed
    • getHtmlContent

      Gets the htmlContent if any.
      Returns:
      Returns the htmlContent if any
    • getMimeMessage

      public jakarta.mail.internet.MimeMessage getMimeMessage()
      Gets the MimeMessage.
      Returns:
      Returns the mimeMessage.
    • getPlainContent

      Gets the plain content if any.
      Returns:
      Returns the plainContent if any
    • getReplyTo

      public String getReplyTo() throws jakarta.mail.MessagingException
      Gets the 'replyTo' address of the email.
      Returns:
      the 'replyTo' address of the email
      Throws:
      jakarta.mail.MessagingException - parsing the mime message failed
    • getSubject

      public String getSubject() throws jakarta.mail.MessagingException
      Gets the MIME message subject.
      Returns:
      the MIME message subject.
      Throws:
      jakarta.mail.MessagingException - parsing the mime message failed.
    • getTo

      public List<jakarta.mail.Address> getTo() throws jakarta.mail.MessagingException
      Gets the MIME message 'to' list.
      Returns:
      the 'to' recipients of the message.
      Throws:
      jakarta.mail.MessagingException - determining the recipients failed
    • hasAttachments

      public boolean hasAttachments()
      Tests if attachments are present.
      Returns:
      true if attachments are present.
    • hasHtmlContent

      public boolean hasHtmlContent()
      Tests is HTML content is present.
      Returns:
      true if HTML content is present.
    • hasPlainContent

      public boolean hasPlainContent()
      Tests is plain content is present.
      Returns:
      true if a plain content is present.
    • isMultipart

      public boolean isMultipart()
      Tests whether this is multipart.
      Returns:
      Returns the isMultiPart.
    • parse

      public MimeMessageParser parse() throws jakarta.mail.MessagingException, IOException
      Does the actual extraction.
      Returns:
      this instance
      Throws:
      jakarta.mail.MessagingException - parsing the mime message failed
      IOException - parsing the mime message failed
    • parse

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