public final class MimeMessageUtility
extends java.lang.Object
MimeMessageUtility - Utilities for MIME messages.| Modifier and Type | Field and Description |
|---|---|
static javax.mail.internet.InternetAddress |
POISON_ADDRESS
The special poison address that denies a message being sent via
MailTransport.sendMailMessage(com.openexchange.mail.dataobjects.compose.ComposedMailMessage, com.openexchange.mail.dataobjects.compose.ComposeType, Address[])
. |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
checkNonAscii(java.lang.String rawHeader)
Checks if given raw header contains non-ascii characters.
|
static boolean |
containsContentId(java.lang.String contentId,
java.util.Collection<java.lang.String> contentIds)
Checks if given Content-Id value is contained in specified collection.
|
static java.lang.String |
decodeEnvelopeHeader(java.lang.String value)
Decodes a string header obtained from ENVELOPE fetch item.
|
static java.lang.String |
decodeEnvelopeSubject(java.lang.String subject)
Decodes a "Subject" header obtained from ENVELOPE fetch item.
|
static java.lang.String |
decodeMultiEncodedHeader(java.lang.String headerValue)
Decodes a multi-mime-encoded header value using the algorithm specified in RFC 2047, Section 6.1.
|
static java.lang.String |
detectPartCharset(javax.mail.Part p)
Detects the charset of specified part.
|
static java.io.File |
dropInvalidHeaders(java.io.File file,
java.io.File newTempFile)
Drops invalid "X-Original-Headers" header from RFC822 source file.
|
static boolean |
equalsCID(java.lang.String contentId1,
java.lang.String contentId2)
Compares (case insensitive) the given values of message header "Content-ID".
|
static boolean |
equalsCID(java.lang.String contentId1,
java.lang.String contentId2,
java.lang.String ignorableSuffix)
Compares (case insensitive) the given values of message header "Content-ID".
|
static java.lang.String |
extractHeader(java.lang.String headerName,
java.io.InputStream inputStream,
boolean closeStream)
Gets the matching header out of RFC 822 data input stream.
|
static java.lang.String |
fold(int used,
java.lang.String foldMe)
Folds a string at linear whitespace so that each line is no longer than 76 characters, if possible.
|
static java.lang.String |
foldContentDisposition(java.lang.String contentDisposition)
Folds specified
Content-Disposition value. |
static java.lang.String |
foldContentType(java.lang.String contentType)
Folds specified
Content-Type value. |
static java.lang.String |
getCharset(MailPart mailPart,
ContentType contentType)
Gets the appropriate charset encoding for specified mail part.
|
static java.util.List<java.lang.String> |
getContentIDs(java.lang.CharSequence htmlContent)
Gathers all occurring content IDs in HTML content and returns them as a list
|
static javax.mail.internet.MailDateFormat |
getDefaultMailDateFormat()
Gets the default
MailDateFormat. |
static java.lang.String |
getFileName(MailPart mailPart)
Get the decoded filename associated with specified mail part.
|
static java.lang.String |
getHeader(java.lang.String name,
java.lang.String defaultValue,
javax.mail.Part part)
Gets the denoted (single) header.
|
static javax.mail.internet.MailDateFormat |
getMailDateFormat(com.openexchange.session.Session session)
Gets the
MailDateFormat for specified session. |
static javax.mail.internet.MailDateFormat |
getMailDateFormat(java.lang.String timeZoneId)
Gets the
MailDateFormat for specified time zone identifier. |
static javax.mail.Multipart |
getMultipartContentFrom(javax.mail.Part part)
Gets the multipart content from specified part.
|
static javax.mail.Multipart |
getMultipartContentFrom(javax.mail.Part part,
java.lang.String contentType)
Gets the multipart content from specified part.
|
static java.lang.String |
getPlainContentId(java.lang.String contentId)
Gets the plain Content-Id value; meaning possible starting
'<' and trailing '>' stripped off. |
static java.lang.String |
getRealFilename(MailPart part)
Determines specified part's real filename if any available.
|
static java.io.InputStream |
getStreamFromMailPart(MailPart part)
Gets the stream of specified part's raw data.
|
static java.io.InputStream |
getStreamFromPart(javax.mail.Part part)
Gets the stream of specified part's raw data.
|
static boolean |
hasAttachments(com.sun.mail.imap.protocol.BODYSTRUCTURE bodystructure)
Checks if given BODYSTRUCTURE item indicates to contain (file) attachments
|
static boolean |
hasAttachments(MailPart mp,
java.lang.String subtype)
Checks if given multipart contains (file) attachments
|
static boolean |
hasAttachments(javax.mail.Multipart mp,
java.lang.String subtype)
Checks if given multipart contains (file) attachments
|
static boolean |
hasEmbeddedImages(java.lang.CharSequence htmlContent)
Detects if given HTML content contains inlined images
Example:
<img src="cid:s345asd845@12drg">
|
static boolean |
hasReferencedLocalImages(java.lang.CharSequence htmlContent)
Detects if given HTML content contains references to local image files
Example for an uploaded image file referenced within a composed mail:
<img src="/ajax/file? |
static boolean |
isEmptyHeader(java.lang.String[] headers)
Checks if specified headers are empty.
|
static boolean |
isInline(javax.mail.Part part)
Checks if given part is considered as an inline part, that is if:
Part's disposition is equal to "inline" OR
Part has no (file) name
|
static boolean |
isValidImageUri(java.lang.String imageTag)
Checks if specified <image> tag's
src attribute seems to point to OX image Servlet. |
static javax.mail.internet.InternetAddress[] |
parseAddressList(java.lang.String addresslist,
boolean strict)
Parse the given sequence of addresses into InternetAddress objects by invoking
. |
static javax.mail.internet.InternetAddress[] |
parseAddressList(java.lang.String addresslist,
boolean strict,
boolean failOnError)
Parse the given sequence of addresses into InternetAddress objects by invoking
. |
static java.lang.String |
quotePersonal(java.lang.String personal)
Quotes given personal part of an Internet address according to RFC 822 syntax if needed; otherwise the personal is returned
unchanged.
|
static java.lang.String |
quotePhrase(java.lang.String phrase,
boolean encode)
Quotes given phrase if needed.
|
static java.lang.String |
quotePhrase(java.lang.String phrase,
boolean encode,
boolean allowNonAscii)
Quotes given phrase if needed.
|
static java.lang.String |
quoteReplacement(java.lang.String s)
Returns a literal replacement
String for the specified String. |
static java.lang.String |
readContent(MailPart mailPart,
ContentType contentType)
Reads the textual content from specified part.
|
static boolean |
shouldRetry(com.openexchange.exception.OXException e)
Checks whether another attempt to load content of a message and/or part should be performed.
|
static java.lang.String |
unfold(java.lang.String headerLine)
Unfolds a folded header.
|
static void |
writeHeaders(MailPart p,
java.io.OutputStream os)
Writes specified part's headers to given output stream.
|
static void |
writeHeaders(javax.mail.internet.MimePart p,
java.io.OutputStream os)
Writes specified part's headers to given output stream.
|
static void |
writeHeaders(javax.mail.Part p,
java.io.OutputStream os)
Writes specified part's headers to given output stream.
|
public static final javax.mail.internet.InternetAddress POISON_ADDRESS
MailTransport.sendMailMessage(com.openexchange.mail.dataobjects.compose.ComposedMailMessage, com.openexchange.mail.dataobjects.compose.ComposeType, Address[])
.public static boolean shouldRetry(com.openexchange.exception.OXException e)
e - The exception to checktrue to retry; otherwise falsepublic static javax.mail.internet.MailDateFormat getDefaultMailDateFormat()
MailDateFormat.
Note that returned instance of MailDateFormat is shared, therefore use a surrounding synchronized block to preserve thread
safety:
...
final MailDateFormat mdf = MIMEMessageUtility.getMailDateFormat(session);
synchronized(mdf) {
mimeMessage.setHeader("Date", mdf.format(sendDate));
}
...
MailDateFormat for specified sessionpublic static javax.mail.internet.MailDateFormat getMailDateFormat(com.openexchange.session.Session session)
throws com.openexchange.exception.OXException
MailDateFormat for specified session.
Note that returned instance of MailDateFormat is shared, therefore use a surrounding synchronized block to preserve thread
safety:
...
final MailDateFormat mdf = MIMEMessageUtility.getMailDateFormat(session);
synchronized(mdf) {
mimeMessage.setHeader("Date", mdf.format(sendDate));
}
...
session - The user sessionMailDateFormat for specified sessioncom.openexchange.exception.OXException - If MailDateFormat cannot be returnedpublic static javax.mail.internet.MailDateFormat getMailDateFormat(java.lang.String timeZoneId)
throws com.openexchange.exception.OXException
MailDateFormat for specified time zone identifier.
Note that returned instance of MailDateFormat is shared, therefore use a surrounding synchronized block to preserve thread
safety:
...
final MailDateFormat mdf = MIMEMessageUtility.getMailDateFormat(timeZoneId);
synchronized(mdf) {
mimeMessage.setHeader("Date", mdf.format(sendDate));
}
...
timeZoneId - The time zone identifierMailDateFormat for specified time zone identifiercom.openexchange.exception.OXException - If MailDateFormat cannot be returnedpublic static boolean isEmptyHeader(java.lang.String[] headers)
null
if no headers exist.headers - The values for a certain headertrue if specified headers are empty; otherwise falsepublic static boolean hasEmbeddedImages(java.lang.CharSequence htmlContent)
Example:
<img src="cid:s345asd845@12drg">
htmlContent - The HTML contenttrue if given HTML content contains inlined images; otherwise falsepublic static java.util.List<java.lang.String> getContentIDs(java.lang.CharSequence htmlContent)
htmlContent - The HTML contentList containing all occurring content IDspublic static boolean containsContentId(java.lang.String contentId,
java.util.Collection<java.lang.String> contentIds)
contentId - The Content-Id valuecontentIds - The collectiontrue if contained; otherwise falsepublic static java.lang.String getPlainContentId(java.lang.String contentId)
'<' and trailing '>' stripped off.contentId - The Content-Id value to processpublic static boolean equalsCID(java.lang.String contentId1,
java.lang.String contentId2)
<' and
' >' are ignored during comparisoncontentId1 - The first content IDcontentId2 - The second content IDtrue if both are equal; otherwise falsepublic static boolean equalsCID(java.lang.String contentId1,
java.lang.String contentId2,
java.lang.String ignorableSuffix)
<' and
' >' are ignored during comparisoncontentId1 - The first content IDcontentId2 - The second content IDignorableSuffix - The optional ignorable suffix; e.g. "@Open-Xchange"true if both are equal; otherwise falsepublic static boolean isValidImageUri(java.lang.String imageTag)
src attribute seems to point to OX image Servlet.imageTag - The <image> tagtrue if image Servlet is addressed; otherwise falsepublic static boolean hasReferencedLocalImages(java.lang.CharSequence htmlContent)
<img src="/ajax/file?action=get&session=abcdefg&id=123dfr567zh">
<img src="/ajax/image?uid=12gf356j7">
htmlContent - The HTML contenttrue if given HTML content contains references to local image files; otherwise falsepublic static java.lang.String getRealFilename(MailPart part)
part - The part whose filename shall be determinednull if none presentpublic static boolean hasAttachments(MailPart mp, java.lang.String subtype) throws javax.mail.MessagingException, com.openexchange.exception.OXException, java.io.IOException
mp - The multipart to examinesubtype - The multipart's subtypetrue if given multipart contains (file) attachments; otherwise falsejavax.mail.MessagingException - If a messaging error occurscom.openexchange.exception.OXException - If a mail error occursjava.io.IOException - If an I/O error occurspublic static boolean hasAttachments(javax.mail.Multipart mp,
java.lang.String subtype)
throws javax.mail.MessagingException,
com.openexchange.exception.OXException,
java.io.IOException
mp - The multipart to examinesubtype - The multipart's subtypetrue if given multipart contains (file) attachments; otherwise falsejavax.mail.MessagingException - If a messaging error occurscom.openexchange.exception.OXException - If a mail error occursjava.io.IOException - If an I/O error occurspublic static boolean hasAttachments(com.sun.mail.imap.protocol.BODYSTRUCTURE bodystructure)
bodystructure - The BODYSTRUCTURE itemtrue if given BODYSTRUCTURE item indicates to contain (file) attachments; otherwise falsepublic static java.lang.String decodeEnvelopeSubject(java.lang.String subject)
subject - The subject obtained from ENVELOPE fetch itempublic static java.lang.String decodeEnvelopeHeader(java.lang.String value)
value - The header valuepublic static java.lang.String decodeMultiEncodedHeader(java.lang.String headerValue)
If the charset-conversion fails for any sequence, an UnsupportedEncodingException is thrown.
If the String is not a RFC 2047 style encoded header, it is returned as-is
headerValue - The possibly encoded header valuepublic static java.lang.String checkNonAscii(java.lang.String rawHeader)
rawHeader - The raw headerpublic static java.lang.String getFileName(MailPart mailPart)
Returns the value of the "filename" parameter from the "Content-Disposition" header field. If its not available, returns the value of
the "name" parameter from the "Content-Type" header field. Returns null if both are absent.
mailPart - The mail part whose filename shall be returnednull.public static javax.mail.internet.InternetAddress[] parseAddressList(java.lang.String addresslist,
boolean strict)
InternetAddress.parse(String, boolean). If strict is false, simple email addresses separated by
spaces are also allowed. If strict is true, many (but not all) of the RFC822 syntax rules are enforced. In particular,
even if strict is true, addresses composed of simple names (with no "@domain" part) are allowed. Such "illegal"
addresses are not uncommon in real messages.
Non-strict parsing is typically used when parsing a list of mail addresses entered by a human. Strict parsing is typically used when parsing address headers in mail messages.
Additionally the personal parts are MIME encoded using default MIME charset.
addresslist - - comma separated address stringsstrict - - true to enforce RFC822 syntax; otherwise falseInternetAddress objectspublic static javax.mail.internet.InternetAddress[] parseAddressList(java.lang.String addresslist,
boolean strict,
boolean failOnError)
throws javax.mail.internet.AddressException
InternetAddress.parse(String, boolean). If strict is false, simple email addresses separated by
spaces are also allowed. If strict is true, many (but not all) of the RFC822 syntax rules are enforced. In particular,
even if strict is true, addresses composed of simple names (with no "@domain" part) are allowed. Such "illegal"
addresses are not uncommon in real messages.
Non-strict parsing is typically used when parsing a list of mail addresses entered by a human. Strict parsing is typically used when parsing address headers in mail messages.
Additionally the personal parts are MIME encoded using default MIME charset.
addresslist - - comma separated address stringsstrict - - true to enforce RFC822 syntax; otherwise falsefailOnError - - true to fail if parsing fails; otherwise false to get a plain-text representationInternetAddress objectsjavax.mail.internet.AddressException - If parsing fails and failOnError is truepublic static java.lang.String quoteReplacement(java.lang.String s)
String for the specified String. This method produces a String
that will work as a literal replacement s in the appendReplacement method of the Matcher class. The
String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and
dollar signs ('$') will be given no special meaning.s - The string to be literalizedpublic static java.lang.String quotePersonal(java.lang.String personal)
This method guarantees that the resulting string can be used to build an Internet address according to RFC 822 syntax so that the
constructor won't throw an instance of InternetAddress.parse(String).
AddressException
final String quotedPersonal = quotePersonal("Doe, Jane");
final String buildAddr = quotedPersonal + " <someone@somewhere.com>";
System.out.println(buildAddr);
// Plain Address: "=?UTF-8?Q?Doe=2C_Jan=C3=A9?=" <someone@somewhere.com>
final InternetAddress ia = new InternetAddress(buildAddr);
System.out.println(ia.toUnicodeString());
// Unicode Address: "Doe, Jane" <someone@somewhere.com>
personal - The personal's string representationpublic static java.lang.String quotePhrase(java.lang.String phrase,
boolean encode)
phrase - The phraseencode - true to encode phrase according to RFC 822 syntax if needed; otherwise falsepublic static java.lang.String quotePhrase(java.lang.String phrase,
boolean encode,
boolean allowNonAscii)
phrase - The phraseencode - true to encode phrase according to RFC 822 syntax if needed; otherwise falseallowNonAscii - Whether non-ascci characters need quoting or notpublic static java.lang.String foldContentType(java.lang.String contentType)
Content-Type value.contentDisposition - The Content-Type valueContent-Type valuepublic static java.lang.String foldContentDisposition(java.lang.String contentDisposition)
Content-Disposition value.contentDisposition - The Content-Disposition valueContent-Disposition valuepublic static java.lang.String fold(int used,
java.lang.String foldMe)
Note that line breaks in the string aren't escaped; they probably should be.
used - The characters used in line so farfoldMe - The string to foldpublic static java.lang.String unfold(java.lang.String headerLine)
headerLine - The header line to unfoldpublic static java.lang.String extractHeader(java.lang.String headerName,
java.io.InputStream inputStream,
boolean closeStream)
throws java.io.IOException
headerName - The header nameinputStream - The input streamcloseStream - true to close the stream on finish; otherwise falsejava.io.IOException - If reading input stream failspublic static void writeHeaders(MailPart p, java.io.OutputStream os) throws com.openexchange.exception.OXException
p - The partos - The output streamcom.openexchange.exception.OXException - If an I/O error occurspublic static void writeHeaders(javax.mail.Part p,
java.io.OutputStream os)
throws com.openexchange.exception.OXException
p - The partos - The output streamcom.openexchange.exception.OXException - If an error occurspublic static void writeHeaders(javax.mail.internet.MimePart p,
java.io.OutputStream os)
throws com.openexchange.exception.OXException
p - The partos - The output streamcom.openexchange.exception.OXException - If an error occurspublic static java.lang.String getHeader(java.lang.String name,
java.lang.String defaultValue,
javax.mail.Part part)
throws javax.mail.MessagingException
name - The header namedefaultValue - The default value to return if absentpart - The part to look-updefaultValuejavax.mail.MessagingException - If returning header failspublic static boolean isInline(javax.mail.Part part)
throws javax.mail.MessagingException
"inline" OR part - The part to checktrue if inline; otherwise falsejavax.mail.MessagingException - If check failspublic static java.io.File dropInvalidHeaders(java.io.File file,
java.io.File newTempFile)
file - The filenewTempFile - The new file (to write cleansed content to)public static java.lang.String getCharset(MailPart mailPart, ContentType contentType) throws com.openexchange.exception.OXException
mailPart - The partcontentType - The part's Content-Typecom.openexchange.exception.OXException - If detecting charset failspublic static java.lang.String readContent(MailPart mailPart, ContentType contentType) throws com.openexchange.exception.OXException, java.io.IOException
mailPart - The mail partcontentType - The content typenull if part does not existcom.openexchange.exception.OXException - If reading content failsjava.io.IOException - If reading content fails with an I/O errorpublic static java.io.InputStream getStreamFromPart(javax.mail.Part part)
throws java.io.IOException
part - Either a message or a body partjava.io.IOException - If an I/O error occurspublic static java.io.InputStream getStreamFromMailPart(MailPart part) throws com.openexchange.exception.OXException
part - Either a message or a body partcom.openexchange.exception.OXException - If an I/O error occurspublic static javax.mail.Multipart getMultipartContentFrom(javax.mail.Part part)
throws javax.mail.MessagingException,
java.io.IOException
part - The partnulljavax.mail.MessagingException - If a messaging error occursjava.io.IOException - If an I/O error occurspublic static javax.mail.Multipart getMultipartContentFrom(javax.mail.Part part,
java.lang.String contentType)
throws javax.mail.MessagingException,
java.io.IOException
part - The partcontentType - The Content-Type header valuenulljavax.mail.MessagingException - If a messaging error occursjava.io.IOException - If an I/O error occurspublic static java.lang.String detectPartCharset(javax.mail.Part p)
throws javax.mail.MessagingException
p - The part whose charset shall be detectedjavax.mail.MessagingException - If an error occurs in part's getter methods