public class MailUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
MailUtils.Attachment
Very simple representation of a mail attachment after it has been
extracted from a message.
|
| Constructor and Description |
|---|
MailUtils() |
| Modifier and Type | Method and Description |
|---|---|
static javax.mail.internet.MimeBodyPart |
createAttachmentMimeBodyPart(String path)
Produces a mimebodypart object from an attachment file path.
|
static javax.mail.internet.MimeBodyPart |
createZippedAttachmentMimeBodyPart(String path) |
static String |
fixMimeEncodedFilename(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 String |
getBody(javax.mail.Message message)
Get the body of the message as a String.
|
static String |
getBody(javax.mail.Message message,
GetBodyOptions options) |
static String |
getContentId(javax.mail.Part part)
Return content id or empty string for given email part.
|
static 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 String |
getContentType(String headerValue)
Helper which extracts the content type from a header value removing parameters and so on.
|
static String |
getLocalHostName()
Get the local host name from InetAddress and return it in a form suitable for use in an email address or Message-ID.
|
static List<String> |
getSenders(javax.mail.Message message)
Returns a List
|
static boolean |
hasRecipient(String matchEmail,
javax.mail.Message message) |
static boolean |
isAttachment(javax.mail.Part part) |
static boolean |
isContentEmpty(javax.mail.Part part)
Tests if the content of the part content is empty.
|
static boolean |
isIpAddress(String hostname)
Returns true if the given host name is just an IP address, not a domain name
|
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(String addresses)
Parse addresses from a comma (and space) separated string into the proper array
|
public static javax.mail.internet.InternetAddress[] parseAddresses(String addresses) throws javax.mail.internet.AddressException
javax.mail.internet.AddressExceptionpublic static String getBody(javax.mail.Message message) throws javax.mail.MessagingException
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.
message - The message to retrieve the body fromjavax.mail.MessagingException - If there was an error getting the content from the messagepublic static String getBody(javax.mail.Message message, GetBodyOptions options) throws javax.mail.MessagingException
javax.mail.MessagingExceptionpublic static MailUtils.Attachment[] getAttachments(javax.mail.Message message) throws javax.mail.MessagingException, IOException
message - the message from which to extract the attachmentsjavax.mail.MessagingExceptionIOExceptionpublic static boolean isAttachment(javax.mail.Part part)
throws javax.mail.MessagingException
javax.mail.MessagingExceptionpublic static boolean hasRecipient(String matchEmail, javax.mail.Message message) throws javax.mail.MessagingException
javax.mail.MessagingExceptionpublic static List<String> getSenders(javax.mail.Message message) throws javax.mail.MessagingException
message - the message from which to get senders.javax.mail.MessagingException - if the senders can't be retrieved from message.public static javax.mail.internet.MimeBodyPart createAttachmentMimeBodyPart(String path) throws javax.mail.MessagingException
path - javax.mail.MessagingExceptionpublic static javax.mail.internet.MimeBodyPart createZippedAttachmentMimeBodyPart(String path) throws javax.mail.MessagingException
javax.mail.MessagingExceptionpublic static boolean isPartHtml(javax.mail.Part part)
throws javax.mail.MessagingException
part - The part being tested.javax.mail.MessagingException - if javamail complainspublic static boolean isPartPlainText(javax.mail.Part part)
throws javax.mail.MessagingException
part - The part being tested.javax.mail.MessagingException - if javamail complainspublic static boolean isPartMessageType(javax.mail.Part part)
throws javax.mail.MessagingException
part - The part being tested.javax.mail.MessagingException - if javamail complainspublic static boolean isPartRelated(javax.mail.Part part)
throws javax.mail.MessagingException
part - The part being tested.javax.mail.MessagingException - if javamail complainspublic static String getContentType(javax.mail.Part part) throws javax.mail.MessagingException
part - the mail part to extract the content-type from.javax.mail.MessagingException - if retrieving the part's Content-Type header failspublic static String getContentType(String headerValue)
headerValue - The header value.public static boolean isContentEmpty(javax.mail.Part part)
throws javax.mail.MessagingException,
IOException
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.
part - a mail part - may or may not have content.javax.mail.MessagingException - if retrieving content fails.IOException - if retrieving content fails or reading content input stream fails.public static boolean isPartInline(javax.mail.Part part)
throws javax.mail.MessagingException
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.
part - The part being tested.javax.mail.MessagingException - as thrown by java mailpublic static String getContentId(javax.mail.Part part) throws javax.mail.MessagingException
part - the email partjavax.mail.MessagingExceptionpublic static boolean isPartAttachment(javax.mail.Part part)
throws javax.mail.MessagingException
part - The part being tested.javax.mail.MessagingException - if javamail complainspublic static String fixMimeEncodedFilename(String filename) throws IOException
Very probably a MIME-encoded filename - see http://java.sun.com/products/javamail/FAQ.html#encodefilename
filename - IOException - MimeUtility.decodeText(java.lang.String)public static boolean isPartSignaturePKCS7(javax.mail.Part part)
throws javax.mail.MessagingException
part - a mail part. The part is assumed to have a content-type header.javax.mail.MessagingException - if retrieving the Content-Type from the part fails.public static String getLocalHostName()
public static boolean isIpAddress(String hostname)
RFC 1123: 'a dotted-decimal number must be enclosed within "[ ]" brackets for SMTP mail'
Note that this method returning true does not guarantee that the value is a valid IP address, it just states that it is not a valid domain name, and it only contains valid characters for an IP address.
hostname - the host name to checkCopyright © 2018 Atlassian. All rights reserved.