public final class MimeMessageConverter
extends java.lang.Object
MimeMessageConverter - Provides several methods to convert instances of MimeMessage to MailMessage in vice versa.| Modifier and Type | Field and Description |
|---|---|
static int |
BEHAVIOR_CLONE
Indicates to clone passed mail.
|
static int |
BEHAVIOR_STREAM2FILE
Indicates to stream content to a (managed) file.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
checkFieldExistence(MailMessage[] mails,
MailField[] fields)
Checks field existence.
|
static javax.mail.Message |
convertComposedMailMessage(ComposedMailMessage composedMail)
Converts given instance of
ComposedMailMessage into a JavaMail-conform Message object. |
static javax.mail.Flags |
convertMailFlags(int flags)
Converts specified flags bit mask to an instance of
Flags. |
static javax.mail.Message |
convertMailMessage(MailMessage mail)
Converts given instance of
MailMessage into a JavaMail-conform Message object. |
static javax.mail.Message |
convertMailMessage(MailMessage mail,
boolean clone)
Converts given instance of
MailMessage into a JavaMail-conform Message object. |
static javax.mail.Message |
convertMailMessage(MailMessage mail,
int behavior)
Converts given instance of
MailMessage into a JavaMail-conform Message object. |
static javax.mail.Message[] |
convertMailMessages(MailMessage[] mails)
Converts given instances of
MailMessage into JavaMail-conform Message objects. |
static javax.mail.Message[] |
convertMailMessages(MailMessage[] mails,
boolean clone)
Converts given instances of
MailMessage into JavaMail-conform Message objects. |
static javax.mail.Message[] |
convertMailMessages(MailMessage[] mails,
int behavior)
Converts given instances of
MailMessage into JavaMail-conform Message objects. |
static javax.mail.Part |
convertMailPart(MailPart mailPart)
Creates a
Part object from given instance of MailPart. |
static MailMessage |
convertMessage(byte[] asciiBytes)
Creates a message data object from given message bytes conform to RFC822.
|
static MailMessage |
convertMessage(byte[] asciiBytes,
java.lang.String uid,
java.lang.String fullname,
char separator,
MailField[] fields)
Creates a message data object from given
message/rfc822 content filled with desired fields. |
static MailMessage |
convertMessage(java.io.InputStream in)
Creates a message data object from given message bytes conform to RFC822.
|
static MailMessage |
convertMessage(javax.mail.internet.MimeMessage msg)
Creates a message data object from given MIME message.
|
static MailMessage |
convertMessage(javax.mail.internet.MimeMessage msg,
boolean considerFolder)
Creates a message data object from given MIME message.
|
static MailMessage |
convertMessage(javax.mail.internet.MimeMessage msg,
java.lang.String uid,
java.lang.String fullname,
char separator,
MailField[] fields)
Creates a message data object from given MIME message filled with desired fields.
|
static MailMessage[] |
convertMessages(javax.mail.Message[] msgs,
javax.mail.Folder folder,
MailField[] fields,
boolean includeBody)
Converts given array of
Message instances to an array of MailMessage instances. |
static MailMessage[] |
convertMessages(javax.mail.Message[] msgs,
MailField[] fields)
Converts given array of
Message instances to an array of MailMessage instances. |
static MailMessage[] |
convertMessages(javax.mail.Message[] msgs,
MailField[] fields,
java.lang.String[] headerNames,
boolean includeBody)
Converts given array of
Message instances to an array of MailMessage instances. |
static MailPart |
convertPart(byte[] asciiBytes)
Creates a MIME mail part object from given raw bytes.
|
static MailPart |
convertPart(javax.mail.Part part)
Creates a MIME mail part object from given MIME part.
|
static MailPart |
convertPart(javax.mail.Part part,
boolean enforeSize)
Creates a MIME mail part object from given MIME part.
|
static MailMessage |
fillComposedMailMessage(ComposedMailMessage composedMail)
Fills specified instance of
ComposedMailMessage with MimeMessageFiller. |
static javax.mail.internet.InternetAddress[] |
getAddressHeader(java.lang.String addresses)
Gets the address header from given address header value.
|
static javax.mail.internet.InternetAddress[] |
getAddressHeader(java.lang.String name,
MailMessage message)
Gets the address headers denoted by specified header name in a safe manner.
|
static javax.mail.internet.InternetAddress[] |
getAddressHeader(java.lang.String name,
javax.mail.Message message)
Gets the address headers denoted by specified header name in a safe manner.
|
static ContentType |
getContentType(javax.mail.Part part)
Gets the Content-Type for given part.
|
static java.util.Date |
getSentDate(MailPart part)
Returns the value of the RFC 822 "Date" field.
|
static java.lang.String |
getStringHeader(java.lang.String name,
MailMessage message)
Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
|
static java.lang.String |
getStringHeader(java.lang.String name,
MailMessage message,
char delimiter)
Gets the headers denoted by specified header name and decodes its value to a unicode string if necessary.
|
static java.lang.String |
getStringHeader(java.lang.String name,
javax.mail.Message message)
Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
|
static java.lang.String |
getStringHeader(java.lang.String name,
javax.mail.Message message,
char delimiter)
Gets the headers denoted by specified header name and decodes its value to a unicode string if necessary.
|
static java.lang.String |
getSubject(MailMessage message)
Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
|
static java.lang.String |
getSubject(javax.mail.Message message)
Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
|
static HeaderCollection |
loadHeaders(byte[] bytes)
Parses given headers'
byte array into a collection until EOF or 2 subsequent CRLFs occur. |
static HeaderCollection |
loadHeaders(java.io.InputStream inputStream)
Parses given headers'
input stream into a collection until EOF or 2 subsequent CRLFs
occur. |
static HeaderCollection |
loadHeaders(java.lang.String messageSrc)
Parses given message source's headers into a
collection until EOF or 2 subsequent CRLFs occur. |
static javax.mail.Multipart |
multipartFor(javax.mail.internet.MimeMessage message)
Gets the multipart from passed message.
|
static javax.mail.Multipart |
multipartFor(javax.mail.internet.MimeMessage message,
ContentType contentType)
Gets the multipart from passed message.
|
static javax.mail.Multipart |
multipartFor(java.lang.Object content,
ContentType contentType)
Gets the multipart from passed content object.
|
static MailMessage |
newMailMessage()
Returns a new instance of
MailMessage ready to get filled with header and/or flag information, but not capable to reference
to body content. |
static void |
parseFlags(javax.mail.Flags flags,
MailMessage mailMessage)
Parses specified
flags to given mail. |
static int |
parseImportance(java.lang.String importance)
Parses the value of header
Importance. |
static void |
parseImportance(java.lang.String importance,
MailMessage mailMessage)
Parses the value of header
Importance. |
static void |
parseMimeFlags(int flags,
javax.mail.Message msg)
Parses specified flags to given message.
|
static int |
parsePriority(java.lang.String priorityStr)
Parses the value of header
X-Priority. |
static void |
parsePriority(java.lang.String priorityStr,
MailMessage mailMessage)
Parses the value of header
X-Priority. |
static void |
saveChanges(javax.mail.internet.MimeMessage mimeMessage)
Performs
saveChanges() on specified message with sanitizing for a possibly corrupt/wrong
Content-Type header. |
static void |
saveChanges(javax.mail.internet.MimeMessage mimeMessage,
java.lang.String hostName)
Performs
saveChanges() on specified message with sanitizing for a possibly corrupt/wrong Content-Type header. |
public static final int BEHAVIOR_CLONE
public static final int BEHAVIOR_STREAM2FILE
public static javax.mail.Part convertMailPart(MailPart mailPart) throws com.openexchange.exception.OXException
Part object from given instance of MailPart.mailPart - The instance of MailPartPartcom.openexchange.exception.OXExceptionpublic static javax.mail.Message[] convertMailMessages(MailMessage[] mails) throws com.openexchange.exception.OXException
MailMessage into JavaMail-conform Message objects.
Note: This is just a convenience method that invokes convertMailMessage(MailMessage) for each instance of
MailMessage
mails - The source instances of MailMessageMessage objects.com.openexchange.exception.OXException - If conversion failspublic static javax.mail.Message[] convertMailMessages(MailMessage[] mails, boolean clone) throws com.openexchange.exception.OXException
MailMessage into JavaMail-conform Message objects.
Note: This is just a convenience method that invokes convertMailMessage(MailMessage) for each instance of
MailMessage
mails - The source instances of MailMessageclone - true to clone message source; otherwise false to return a reference if possibleMessage objects.com.openexchange.exception.OXException - If conversion failspublic static javax.mail.Message[] convertMailMessages(MailMessage[] mails, int behavior) throws com.openexchange.exception.OXException
MailMessage into JavaMail-conform Message objects.
Note: This is just a convenience method that invokes convertMailMessage(MailMessage) for each instance of
MailMessage
mails - The source instances of MailMessagebehavior - Provides the behavior bitsMessage objects.com.openexchange.exception.OXException - If conversion failsBEHAVIOR_CLONE,
BEHAVIOR_STREAM2FILEpublic static javax.mail.Message convertMailMessage(MailMessage mail) throws com.openexchange.exception.OXException
MailMessage into a JavaMail-conform Message object.mail - The source instance of MailMessageMessage objectcom.openexchange.exception.OXException - If conversion failspublic static javax.mail.Message convertMailMessage(MailMessage mail, boolean clone) throws com.openexchange.exception.OXException
MailMessage into a JavaMail-conform Message object.mail - The source instance of MailMessageclone - true to clone message source; otherwise false to return a reference if possibleMessage objectcom.openexchange.exception.OXException - If conversion failspublic static javax.mail.Message convertMailMessage(MailMessage mail, int behavior) throws com.openexchange.exception.OXException
MailMessage into a JavaMail-conform Message object.mail - The source instance of MailMessagebehavior - Provides the behavior bitsMessage objectcom.openexchange.exception.OXException - If conversion failsBEHAVIOR_CLONE,
BEHAVIOR_STREAM2FILEpublic static javax.mail.Message convertComposedMailMessage(ComposedMailMessage composedMail) throws com.openexchange.exception.OXException
ComposedMailMessage into a JavaMail-conform Message object.composedMail - The source instance of ComposedMailMessageMessage objectcom.openexchange.exception.OXException - If conversion failspublic static MailMessage fillComposedMailMessage(ComposedMailMessage composedMail) throws com.openexchange.exception.OXException
ComposedMailMessage with MimeMessageFiller.composedMail - The composed mailMailMessage ready for further usagecom.openexchange.exception.OXException - If mail cannot be filled.public static javax.mail.Multipart multipartFor(javax.mail.internet.MimeMessage message)
throws com.openexchange.exception.OXException
message - The messagecom.openexchange.exception.OXException - If content cannot be presented as a multipartpublic static javax.mail.Multipart multipartFor(javax.mail.internet.MimeMessage message,
ContentType contentType)
throws com.openexchange.exception.OXException
message - The messagecontentType - The message's Content-Typecom.openexchange.exception.OXException - If content cannot be presented as a multipartpublic static ContentType getContentType(javax.mail.Part part) throws com.openexchange.exception.OXException
part - The partcom.openexchange.exception.OXException - If parsing failspublic static javax.mail.Multipart multipartFor(java.lang.Object content,
ContentType contentType)
throws com.openexchange.exception.OXException
content - The content objectcontentType - The content typecom.openexchange.exception.OXException - If content cannot be presented as a multipartpublic static void saveChanges(javax.mail.internet.MimeMessage mimeMessage,
java.lang.String hostName)
throws com.openexchange.exception.OXException
saveChanges() on specified message with sanitizing for a possibly corrupt/wrong Content-Type header.
Aligns Message-Id header to given host name.
mimeMessage - The MIME messagehostName - The host namecom.openexchange.exception.OXException - If operation failspublic static void saveChanges(javax.mail.internet.MimeMessage mimeMessage)
throws com.openexchange.exception.OXException
saveChanges() on specified message with sanitizing for a possibly corrupt/wrong
Content-Type header.mimeMessage - The messagecom.openexchange.exception.OXException - If an error occurspublic static void parseMimeFlags(int flags,
javax.mail.Message msg)
throws javax.mail.MessagingException
flags - The flags to parsemsg - The message to filljavax.mail.MessagingException - If a messaging error occurspublic static MailMessage[] convertMessages(javax.mail.Message[] msgs, MailField[] fields) throws com.openexchange.exception.OXException
Message instances to an array of MailMessage instances. The single elements of the array are
expected to be instances of ExtendedMimeMessage; meaning the messages were created through a manual fetch.
Only the fields specified through parameter fields are going to be set
msgs - The source messagesfields - The fields to fillMessage instancescom.openexchange.exception.OXException - If conversion failsto convert common instances of {@link Message}public static MailMessage[] convertMessages(javax.mail.Message[] msgs, MailField[] fields, java.lang.String[] headerNames, boolean includeBody) throws com.openexchange.exception.OXException
Message instances to an array of MailMessage instances. The single elements of the array are
expected to be instances of ExtendedMimeMessage; meaning the messages were created through a manual fetch.
Only the fields specified through parameter fields are going to be set
msgs - The source messagesfields - The fields to fillheaderNames - The header namesincludeBody - Whether to create mail messages with reference to content or notMessage instancescom.openexchange.exception.OXException - If conversion fails#convertMessages(Message[], Folder, MailField[])public static MailMessage[] convertMessages(javax.mail.Message[] msgs, javax.mail.Folder folder, MailField[] fields, boolean includeBody) throws com.openexchange.exception.OXException
Message instances to an array of MailMessage instances.
Only the fields specified through parameter fields are going to be set
msgs - The source messagesfolder - The folder containing source messagesfields - The fields to fillincludeBody - true to include body; otherwise falseMessage instancescom.openexchange.exception.OXException - If conversion failspublic static void checkFieldExistence(MailMessage[] mails, MailField[] fields)
mails - The mails to checksfields - The field to check forpublic static MailMessage newMailMessage()
MailMessage ready to get filled with header and/or flag information, but not capable to reference
to body content.MailMessagepublic static MailMessage convertMessage(java.io.InputStream in) throws com.openexchange.exception.OXException
in - The message input stream conform to RFC822MailMessagecom.openexchange.exception.OXException - If conversion failspublic static MailMessage convertMessage(byte[] asciiBytes) throws com.openexchange.exception.OXException
asciiBytes - The message bytes conform to RFC822MailMessagecom.openexchange.exception.OXException - If conversion failspublic static MailMessage convertMessage(javax.mail.internet.MimeMessage msg) throws com.openexchange.exception.OXException
msg - The MIME messageMailMessage containing the attributes from given MIME messagecom.openexchange.exception.OXException - If conversion failspublic static MailMessage convertMessage(javax.mail.internet.MimeMessage msg, boolean considerFolder) throws com.openexchange.exception.OXException
msg - The MIME messageconsiderFolder - true to consider MIME message's folder (see Message.getFolder()); otherwise falseMailMessage containing the attributes from given MIME messagecom.openexchange.exception.OXException - If conversion failspublic static MailMessage convertMessage(byte[] asciiBytes, java.lang.String uid, java.lang.String fullname, char separator, MailField[] fields) throws com.openexchange.exception.OXException
message/rfc822 content filled with desired fields.asciiBytes - The message/rfc822 contentuid - The UID or -1fullname - The folder's fullnameseparator - The folder's separatorfields - The desired fields to fillMailMessage filled with desired fieldscom.openexchange.exception.OXException - If conversion failspublic static MailMessage convertMessage(javax.mail.internet.MimeMessage msg, java.lang.String uid, java.lang.String fullname, char separator, MailField[] fields) throws com.openexchange.exception.OXException
msg - The MIME messageuid - The UID or nullfullname - The folder fullnameseparator - The folder separator characterfields - The desired fields to fillMailMessage filled with desired fieldscom.openexchange.exception.OXException - If conversion failspublic static MailPart convertPart(byte[] asciiBytes) throws com.openexchange.exception.OXException
asciiBytes - The raw bytescom.openexchange.exception.OXException - If creating MIME mail part object failspublic static MailPart convertPart(javax.mail.Part part) throws com.openexchange.exception.OXException
part - The partMailPart containing the attributes from given partcom.openexchange.exception.OXExceptionpublic static MailPart convertPart(javax.mail.Part part, boolean enforeSize) throws com.openexchange.exception.OXException
part - The partenforeSize - true to ensure size is set in returned mail part; otherwise false. If set given part's
input stream is examined which might unnecessarily load data from backend.MailPart containing the attributes from given partcom.openexchange.exception.OXExceptionpublic static javax.mail.Flags convertMailFlags(int flags)
Flags.flags - The flags bit maskFlagspublic static void parseFlags(javax.mail.Flags flags,
MailMessage mailMessage)
throws com.openexchange.exception.OXException
flags to given mail.flags - The flags to parsemailMessage - The mail to apply the flags tocom.openexchange.exception.OXException - If a mail error occurspublic static HeaderCollection loadHeaders(java.io.InputStream inputStream) throws java.io.IOException
input stream into a collection until EOF or 2 subsequent CRLFs
occur.
This is a convenience method that delegates to loadHeaders(byte[]).
inputStream - The headers' input streamcollection.java.io.IOException - If an I/O error occurs.public static HeaderCollection loadHeaders(byte[] bytes)
byte array into a collection until EOF or 2 subsequent CRLFs occur.
This is a convenience method that delegates to loadHeaders(String).
bytes - The headers' byte arraycollection.public static HeaderCollection loadHeaders(java.lang.String messageSrc)
collection until EOF or 2 subsequent CRLFs occur.messageSrc - The message sourcecollection.public static java.lang.String getSubject(javax.mail.Message message)
throws javax.mail.MessagingException
"=?UTF-8?Q?=C3=BCber?=" is decoded to "über"
name - The header namemessage - The message providing the headerjavax.mail.MessagingException - If a messaging error occurspublic static java.lang.String getSubject(MailMessage message)
"=?UTF-8?Q?=C3=BCber?=" is decoded to "über"
name - The header namemessage - The message providing the headerpublic static java.lang.String getStringHeader(java.lang.String name,
javax.mail.Message message)
throws javax.mail.MessagingException
"=?UTF-8?Q?=C3=BCber?=" is decoded to "über"
name - The header namemessage - The message providing the headerjavax.mail.MessagingException - If a messaging error occurspublic static java.lang.String getStringHeader(java.lang.String name,
MailMessage message)
"=?UTF-8?Q?=C3=BCber?=" is decoded to "über"
name - The header namemessage - The message providing the headerpublic static java.lang.String getStringHeader(java.lang.String name,
javax.mail.Message message,
char delimiter)
throws javax.mail.MessagingException
"=?UTF-8?Q?=C3=BCber?=" is decoded to "über"
name - The header namemessage - The message providing the headerdelimiter - The delimiter character if message contains multiple header values; set to '\0' to only consider first
onejavax.mail.MessagingException - If a messaging error occurspublic static java.lang.String getStringHeader(java.lang.String name,
MailMessage message,
char delimiter)
"=?UTF-8?Q?=C3=BCber?=" is decoded to "über"
name - The header namemessage - The message providing the headerdelimiter - The delimiter character if message contains multiple header values; set to '\0' to only consider first
onepublic static javax.mail.internet.InternetAddress[] getAddressHeader(java.lang.String name,
javax.mail.Message message)
throws javax.mail.MessagingException
If strict parsing of address headers yields a AddressException, then a plain-text version is generated to display broken
address header as it is.
name - The address header namemessage - The message providing the address headerInternetAddress instancesjavax.mail.MessagingException - If a messaging error occurspublic static javax.mail.internet.InternetAddress[] getAddressHeader(java.lang.String name,
MailMessage message)
If strict parsing of address headers yields a AddressException, then a plain-text version is generated to display broken
address header as it is.
name - The address header namemessage - The message providing the address headerInternetAddress instancespublic static javax.mail.internet.InternetAddress[] getAddressHeader(java.lang.String addresses)
addresses - The address header valuepublic static java.util.Date getSentDate(MailPart part)
null if this
field is unavailable or its value is absent.part - The mail partpublic static void parsePriority(java.lang.String priorityStr,
MailMessage mailMessage)
X-Priority.priorityStr - The header valuemailMessage - The mail message to fillpublic static void parseImportance(java.lang.String importance,
MailMessage mailMessage)
Importance.importance - The header valuemailMessage - The mail message to fillpublic static int parsePriority(java.lang.String priorityStr)
X-Priority.priorityStr - The header valuepublic static int parseImportance(java.lang.String importance)
Importance.importance - The header value