Package org.apache.commons.mail.util
Class MimeMessageParser
- java.lang.Object
-
- org.apache.commons.mail.util.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 mimeMessage)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.DataSourcecreateDataSource(javax.mail.Multipart parent, javax.mail.internet.MimePart part)Parses the MimePart to create a DataSource.javax.activation.DataSourcefindAttachmentByCid(java.lang.String cid)Find an attachment using its content-id.javax.activation.DataSourcefindAttachmentByName(java.lang.String name)Find an attachment using its name.java.util.List<javax.activation.DataSource>getAttachmentList()Gets the attachment list.java.util.List<javax.mail.Address>getBcc()Gets the BCC Address list.java.util.List<javax.mail.Address>getCc()Gets the CC Address list.java.util.Collection<java.lang.String>getContentIds()Returns a collection of all content-ids in the parsed message.protected java.lang.StringgetDataSourceName(javax.mail.Part part, javax.activation.DataSource dataSource)Determines the name of the data source if it is not already set.java.lang.StringgetFrom()Gets the FROM field.java.lang.StringgetHtmlContent()Gets the htmlContent if any.javax.mail.internet.MimeMessagegetMimeMessage()Gets the MimeMessage.java.lang.StringgetPlainContent()Gets the plain content if any.java.lang.StringgetReplyTo()Gets the 'replyTo' address of the email.java.lang.StringgetSubject()Gets the MIME message subject.java.util.List<javax.mail.Address>getTo()Gets the MIME message 'to' list.booleanhasAttachments()Tests if attachments are present.booleanhasHtmlContent()Tests is HTML content is present.booleanhasPlainContent()Tests is plain content is present.booleanisMultipart()Tests whether this is multipart.MimeMessageParserparse()Does the actual extraction.protected voidparse(javax.mail.Multipart parent, javax.mail.internet.MimePart part)Extracts the content of a MimeMessage recursively.
-
-
-
Constructor Detail
-
MimeMessageParser
public MimeMessageParser(javax.mail.internet.MimeMessage mimeMessage)
Constructs an instance with the MimeMessage to be extracted.- Parameters:
mimeMessage- the message to parse
-
-
Method Detail
-
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-partpart- the current part to be processed- Returns:
- the DataSource
- Throws:
javax.mail.MessagingException- creating the DataSource failedjava.io.IOException- error getting InputStream or unsupported encoding
-
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
-
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
-
getAttachmentList
public java.util.List<javax.activation.DataSource> getAttachmentList()
Gets the attachment list.- Returns:
- Returns the attachment list.
-
getBcc
public java.util.List<javax.mail.Address> getBcc() throws javax.mail.MessagingException
Gets the BCC Address list.- Returns:
- the 'BCC' recipients of the message
- Throws:
javax.mail.MessagingException- determining the recipients failed
-
getCc
public java.util.List<javax.mail.Address> getCc() throws javax.mail.MessagingException
Gets the CC Address list.- Returns:
- the 'CC' recipients of the message
- Throws:
javax.mail.MessagingException- determining the recipients failed
-
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
-
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 partdataSource- the data source- Returns:
- the name of the data source or
nullif no name can be determined - Throws:
javax.mail.MessagingException- accessing the part failedjava.io.UnsupportedEncodingException- decoding the text failed
-
getFrom
public java.lang.String getFrom() throws javax.mail.MessagingException
Gets the FROM field.- Returns:
- the FROM field of the message
- Throws:
javax.mail.MessagingException- parsing the mime message failed
-
getHtmlContent
public java.lang.String getHtmlContent()
Gets the htmlContent if any.- Returns:
- Returns the htmlContent if any
-
getMimeMessage
public javax.mail.internet.MimeMessage getMimeMessage()
Gets the MimeMessage.- Returns:
- Returns the mimeMessage.
-
getPlainContent
public java.lang.String getPlainContent()
Gets the plain content if any.- Returns:
- Returns the plainContent if any
-
getReplyTo
public java.lang.String getReplyTo() throws javax.mail.MessagingException
Gets the 'replyTo' address of the email.- Returns:
- the 'replyTo' address of the email
- Throws:
javax.mail.MessagingException- parsing the mime message failed
-
getSubject
public java.lang.String getSubject() throws javax.mail.MessagingException
Gets the MIME message subject.- Returns:
- the MIME message subject.
- Throws:
javax.mail.MessagingException- parsing the mime message failed.
-
getTo
public java.util.List<javax.mail.Address> getTo() throws javax.mail.MessagingException
Gets the MIME message 'to' list.- Returns:
- the 'to' recipients of the message.
- Throws:
javax.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 javax.mail.MessagingException, java.io.IOException
Does the actual extraction.- Returns:
- this instance
- Throws:
javax.mail.MessagingException- parsing the mime message failedjava.io.IOException- 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-partpart- the current MimePart- Throws:
javax.mail.MessagingException- parsing the MimeMessage failedjava.io.IOException- parsing the MimeMessage failed
-
-