com.atlassian.mail
Class MailUtils

java.lang.Object
  extended bycom.atlassian.mail.MailUtils

public class MailUtils
extends java.lang.Object

This class contains a bunch of static helper methods that make life a bit easier particularly with the processing of Parts.


Nested Class Summary
static class MailUtils.Attachment
          Very simple representation of a mail attachment after it has been extracted from a message.
 
Constructor Summary
MailUtils()
           
 
Method Summary
static javax.mail.internet.MimeBodyPart createAttachmentMimeBodyPart(java.lang.String path)
          Produces a mimebodypart object from an attachment file path.
static javax.mail.internet.MimeBodyPart createZippedAttachmentMimeBodyPart(java.lang.String path)
           
static java.lang.String fixMimeEncodedFilename(java.lang.String filename)
          This method may be used to fix any mime encoded filenames that have been returned by javamail.
static MailUtils.Attachment[] getAttachments(javax.mail.Message message)
          Gets all parts of a message that are attachments rather than alternative inline bits.
static com.opensymphony.user.User getAuthorFromSender(javax.mail.Message message)
          Deprecated. Now incorporated into JIRA due to app-specific logic, will be removed in a future release. (complain to chris@atlassian.com)
static java.lang.String getBody(javax.mail.Message message)
          Get the body of the message as a String.
static java.lang.String getContentType(javax.mail.Part part)
          Helper which returns the pure mime/subMime content type less any other extra parameters which may accompany the header value.
static java.lang.String getContentType(java.lang.String headerValue)
          Helper which extracts the content type from a header value removing parameters and so on.
static com.opensymphony.user.User getFirstValidUser(javax.mail.Address[] addresses)
          Deprecated. Now incorporated into JIRA due to app-specific logic, will be removed in a future release. (complain to chris@atlassian.com)
static java.util.List getSenders(javax.mail.Message message)
          Returns a List of trimmed non-null email addresses from the given potentially dirty pile of addresses listed as senders on the given message.
static boolean hasRecipient(java.lang.String matchEmail, javax.mail.Message message)
           
static boolean isContentEmpty(javax.mail.Part part)
          Tests if the content of the part content is empty.
static boolean isPartAttachment(javax.mail.Part part)
          Tests if the provided part is an attachment.
static boolean isPartHtml(javax.mail.Part part)
          Tests if a particular part content type is text/html.
static boolean isPartInline(javax.mail.Part part)
          This method uses a number of checks to determine if the given part actually represents an inline (typically image) part.
static boolean isPartMessageType(javax.mail.Part part)
          Tests if the provided part's content type is message/rfc822
static boolean isPartPlainText(javax.mail.Part part)
          Tests if the provided part content type is text/plain.
static boolean isPartRelated(javax.mail.Part part)
          Tests if the provided part's content type is multipart/related
static boolean isPartSignaturePKCS7(javax.mail.Part part)
          Tests if a part is actually a signature.
static javax.mail.internet.InternetAddress[] parseAddresses(java.lang.String addresses)
          Parse addresses from a comma (and space) separated string into the proper array
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MailUtils

public MailUtils()
Method Detail

parseAddresses

public static javax.mail.internet.InternetAddress[] parseAddresses(java.lang.String addresses)
                                                            throws javax.mail.internet.AddressException
Parse addresses from a comma (and space) separated string into the proper array

Throws:
javax.mail.internet.AddressException

getBody

public static java.lang.String getBody(javax.mail.Message message)
                                throws javax.mail.MessagingException
Get the body of the message as a String. The algorithm for finding the body is as follows:
  1. If the message is a single part, and that part is text/plain, return it.
  2. If the message is a single part, and that part is text/html, convert it to text (stripping out the HTML) and return it.
  3. If the message is multi-part, return the first text/plain part that isn't marked explicitly as an attachment.
  4. If the message is multi-part, but does not contain any text/plain parts, return the first text/html part that isn't marked explicitly as an attachment, converting it to text and stripping the HTML.
  5. If nothing is found in any of the steps above, return null.

Note: If the message contains nested multipart parts, an HTML part nested at a higher level will take precedence over a text part nested deeper.

Parameters:
message - The message to retrieve the body from
Returns:
The message body, or null if the message could not be parsed
Throws:
javax.mail.MessagingException - If there was an error getting the content from the message

getAttachments

public static MailUtils.Attachment[] getAttachments(javax.mail.Message message)
                                             throws javax.mail.MessagingException,
                                                    java.io.IOException
Gets all parts of a message that are attachments rather than alternative inline bits.

Parameters:
message - the message from which to extract the attachments
Returns:
an array of the extracted attachments
Throws:
javax.mail.MessagingException
java.io.IOException

getAuthorFromSender

public static com.opensymphony.user.User getAuthorFromSender(javax.mail.Message message)
                                                      throws javax.mail.MessagingException
Deprecated. Now incorporated into JIRA due to app-specific logic, will be removed in a future release. (complain to chris@atlassian.com)

Get the user that has the same email address as the author of the message. If multiple authors, take the first one.

Parameters:
message - The message to get the author from.
Returns:
The user who has the same email address as the author of the message
Throws:
javax.mail.MessagingException - If an error occurred getting the message author

getFirstValidUser

public static com.opensymphony.user.User getFirstValidUser(javax.mail.Address[] addresses)
Deprecated. Now incorporated into JIRA due to app-specific logic, will be removed in a future release. (complain to chris@atlassian.com)

Given an array of addresses, this method returns the first valid address.

Parameters:
addresses - addresses to be used to search for a User.
Returns:
a User for the email address or null.

hasRecipient

public static boolean hasRecipient(java.lang.String matchEmail,
                                   javax.mail.Message message)
                            throws javax.mail.MessagingException
Returns:
true if at least one of the recipients matches the email address given.
Throws:
javax.mail.MessagingException

getSenders

public static java.util.List getSenders(javax.mail.Message message)
                                 throws javax.mail.MessagingException
Returns a List of trimmed non-null email addresses from the given potentially dirty pile of addresses listed as senders on the given message.

Parameters:
message - the message from which to get senders.
Returns:
a nice List of email addresses.
Throws:
javax.mail.MessagingException - if the senders can't be retrieved from message.

createAttachmentMimeBodyPart

public static javax.mail.internet.MimeBodyPart createAttachmentMimeBodyPart(java.lang.String path)
                                                                     throws javax.mail.MessagingException
Produces a mimebodypart object from an attachment file path. An attachment needs to be in this form to be attached to an email for sending

Parameters:
path -
Returns:
Throws:
javax.mail.MessagingException

createZippedAttachmentMimeBodyPart

public static javax.mail.internet.MimeBodyPart createZippedAttachmentMimeBodyPart(java.lang.String path)
                                                                           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

isPartHtml

public static boolean isPartHtml(javax.mail.Part part)
                          throws javax.mail.MessagingException
Tests if a particular part content type is text/html.

Parameters:
part - The part being tested.
Returns:
true if the part content type is text/html
Throws:
javax.mail.MessagingException - if javamail complains

isPartPlainText

public static boolean isPartPlainText(javax.mail.Part part)
                               throws javax.mail.MessagingException
Tests if the provided part content type is text/plain.

Parameters:
part - The part being tested.
Returns:
true if the part content type is text/plain
Throws:
javax.mail.MessagingException - if javamail complains

isPartMessageType

public static boolean isPartMessageType(javax.mail.Part part)
                                 throws javax.mail.MessagingException
Tests if the provided part's content type is message/rfc822

Parameters:
part - The part being tested.
Returns:
true if the part content type is message/rfc822
Throws:
javax.mail.MessagingException - if javamail complains

isPartRelated

public static boolean isPartRelated(javax.mail.Part part)
                             throws javax.mail.MessagingException
Tests if the provided part's content type is multipart/related

Parameters:
part - The part being tested.
Returns:
true if the part content type is multipart/related
Throws:
javax.mail.MessagingException - if javamail complains

getContentType

public static java.lang.String getContentType(javax.mail.Part part)
                                       throws javax.mail.MessagingException
Helper which returns the pure mime/subMime content type less any other extra parameters which may accompany the header value.

Parameters:
part - the mail part to extract the content-type from.
Returns:
the pure mime/subMime type
Throws:
javax.mail.MessagingException - if retrieving the part's Content-Type header fails

getContentType

public static java.lang.String getContentType(java.lang.String headerValue)
Helper which extracts the content type from a header value removing parameters and so on.

Parameters:
headerValue - The header value.
Returns:
The actual content type

isContentEmpty

public static boolean isContentEmpty(javax.mail.Part part)
                              throws javax.mail.MessagingException,
                                     java.io.IOException
Tests if the content of the part content is empty. The definition of empty depends on whether the content is text or binary.

Text content for content types like plain/text and html/text is defined as being empty if it contains an empty string after doing a trim(). If the string contains 50 spaces it is still empty whilst a string with a solitary "a" isnt.

For binary content (like images) if the content contains 0 bytes it is empty whilst anything with 1 or more bytes is NOT considered empty.

Parameters:
part - a mail part - may or may not have content.
Returns:
true/false if the content is deemed empty as per above rules.
Throws:
javax.mail.MessagingException - if retrieving content fails.
java.io.IOException - if retrieving content fails or reading content input stream fails.

isPartInline

public static boolean isPartInline(javax.mail.Part part)
                            throws javax.mail.MessagingException
This method uses a number of checks to determine if the given part actually represents an inline (typically image) part. Some email clients (aka lotus notes) dont appear to correctly set the disposition to inline so a number of additional checks are required, hence the multi staged approached.

eg. inline images from notes wont have a inline disposition but will have a content id and will also have their content base64 encoded. This approach helps us correctly identify inline images or other binary parts.

Parameters:
part - The part being tested.
Returns:
True if the part is inline false in all other cases.
Throws:
javax.mail.MessagingException - as thrown by java mail

isPartAttachment

public static boolean isPartAttachment(javax.mail.Part part)
                                throws javax.mail.MessagingException
Tests if the provided part is an attachment. Note this method does not test if the content is empty etc it merely tests whether or not the part is an attachment of some sort.

Parameters:
part - The part being tested.
Throws:
javax.mail.MessagingException - if javamail complains

fixMimeEncodedFilename

public static java.lang.String fixMimeEncodedFilename(java.lang.String filename)
                                               throws java.io.IOException
This method may be used to fix any mime encoded filenames that have been returned by javamail. No harm can occur from calling this method unnecessarily except for wasting a few cpu cycles...

Very probably a MIME-encoded filename - see http://java.sun.com/products/javamail/FAQ.html#encodefilename

Parameters:
filename -
Returns:
The fixed filename.
Throws:
java.io.IOException - MimeUtility.decodeText(java.lang.String)

isPartSignaturePKCS7

public static boolean isPartSignaturePKCS7(javax.mail.Part part)
                                    throws javax.mail.MessagingException
Tests if a part is actually a signature. This is required to fix JRA-9933.

Parameters:
part - a mail part. The part is assumed to have a content-type header.
Returns:
true if the content-type header matches the standard PKCS7 mime types
Throws:
javax.mail.MessagingException - if retrieving the Content-Type from the part fails.


Copyright © 2008 Atlassian Pty Ltd. All Rights Reserved.