Class MailUtil
Message
objects.-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
adjustImageTags
(String htmlString, List<Part> parts) This method takes a HTML string that contains inline images (i.e.static Message
convertBytesToMessage
(byte[] msgContent) Converts the given Byte Array to a javax.mail.Messagestatic Message
convertBytesToMessage
(byte[] msgContent, Session s) Converts the given Byte Array to a javax.mail.Messagestatic byte[]
Converts the given message to Byte Arraystatic String[]
convertToArray
(String to) Converts the given TO addresses (separated by comma) to an arraystatic MimeMessage
convertToMimeMessage
(org.apache.poi.hsmf.MAPIMessage msg) Method to convertMAPIMessage
toMimeMessage
.static String
decodeText
(String txt) Decodes the given encoded text (e.g. mail subjects).This method returns all parts of a given message that are not of type multipart/*.This method returns all parts of a given message that are not of type multipart/*, but without inline images.static String
extractHtmlBodyContent
(String strHtml) This method takes a HTML string and extracts the content of the body tag, i.e. everything between <body> and </body>.static Part
This method returns the first part of a given message that meets the following conditions:static Part
This method returns the first part of a given message that meets the following conditions:static InternetAddress
extractSender
(Message msg) Extracts the sender address of a given message.static String
extractSubjectId
(String text) The method tries to extract the pattern id of the given text.static String
Returns a HTML encoded string that represents a given email address.static String
getDisplayAddressList
(Address[] addresses) Returns a string containing a list of email addresses like <address-1>, <address-2>, ..., <address-n>,static String
getDisplayAddressList
(Message msg, String field) Returns a string containing a list of email addresses like <address-1>, <address-2>, ..., <address-n>,static String
getDisplayFilename
(Part part) Returns the filename of a given part or "unnamed" if the part does not have a name.static String
getDisplayMailContent
(Message msg, boolean htmlPreferred, boolean onlyBody) This method returns a HTML-encoded mail-content (not only the html-content between body tags)static Address[]
Return the sender information in message header (From or Sender) of given message.static String
getMailSocketFactoryName
(String communicationType) Get the socket factory name of given communication typestatic String
This method returns the subject prefix defined in @enterprise configuration.static String
getMessageId
(Message msg) This method returns the message-id of the given messagestatic String
Get the standard sender defined in configuration.static String
Get SMTP Credentials from configuration.static String
Get the standard SMTP-host defined in configurationstatic boolean
hasAttachments
(Message msg) Method to check, if the message has real attachments and inline images too, i.e. the mail body parts (text/plain and text/html) are not considered.static boolean
Method to check, if the message has real attachments, i.e. the mail body parts (text/plain and text/html) and the inline images are not considered.static String
htmlBodyToPlainText
(String strHtml) This method takes the content of a HTML body, i.e. everything between <body> and </body>, and removes the tags.static String
htmlDecode
(String content) This method replaces entities with their real symbols, i.e.static boolean
isMailReceived
(Message msg) Method to check, if email contains received attribute(s).static boolean
isSecureSMTP
(String type) static boolean
isValidEmailAddress
(String email) Method to validate given email address.static String
Replace sequences of CRLF characters by single spaces.
-
Field Details
-
PLAIN
- See Also:
-
ENCRYPTED
- See Also:
-
TRUSTED
- See Also:
-
STARTTLS
- See Also:
-
-
Method Details
-
convertMessageToBytes
Converts the given message to Byte Array- Parameters:
msg
- the javax.mail.Message- Returns:
- the byte[] of message or
null
, if an error occurred
-
sanitizeCRLF
Replace sequences of CRLF characters by single spaces. Usage recommended to sanitize the subject of mail messages, that is before callingMessage.setSubject(String)
c.f. [CSNC-2014-001]- Parameters:
s
- the String to sanitize- Returns:
- the input string where all occurrences of sequences of CR and LF have been replaced by single spaces.
-
convertBytesToMessage
Converts the given Byte Array to a javax.mail.Message- Parameters:
msgContent
- the content as byte[]- Returns:
- the Message or
null
, if an error occurred
-
convertBytesToMessage
Converts the given Byte Array to a javax.mail.Message- Parameters:
msgContent
- the content as byte[]s
- the SMTP session which shall be used to transport the message- Returns:
- the Message or
null
, if an error occurred
-
getSMTPHost
Get the standard SMTP-host defined in configuration- Returns:
- the SMTP-host of configuration parameter "mail.smtphost" or
null
, if not defined.
-
getSMTPCredentials
Get SMTP Credentials from configuration.- Returns:
- a
Pair
where key contains value of configuration parameter "ep.mail.smtp.username" and value "ep.mail.smtp.password". If no username is set,null
is returned.
-
getSMTPCommunicationType
- Returns:
- the communication type defined in configuration parameter "ep.mail.smtp.communicationtype"
-
isSecureSMTP
-
getSender
Get the standard sender defined in configuration.- Returns:
- the mail sender defined in configuration parameter "mail.sender", otherwise the string "enterprise@<local-hostname>
-
convertToArray
Converts the given TO addresses (separated by comma) to an array- Parameters:
to
- the TO addresses- Returns:
- the addresses as an array or an empty array, if no to parameter is given.
-
getMailSocketFactoryName
Get the socket factory name of given communication type -
convertToMimeMessage
public static MimeMessage convertToMimeMessage(org.apache.poi.hsmf.MAPIMessage msg) throws Exception Method to convert
MAPIMessage
toMimeMessage
.- Parameters:
msg
- theMAPIMessage
object- Returns:
- the converted
MimeMessage
object - Throws:
Exception
-
isValidEmailAddress
Method to validate given email address.- Parameters:
email
- the email address to validate- Returns:
true
, if email is a correct email address
-
getMailSubjectPattern
This method returns the subject prefix defined in @enterprise configuration.- Returns:
- the subject prefix defined with parameter "ep.mail.subjpattern" or
null
, if no parameter value is available
-
getMessageId
This method returns the message-id of the given message- Parameters:
msg
- theMimeMessage
object- Returns:
- the message-id of given message or
null
, if the id could not be determined
-
extractSender
Extracts the sender address of a given message.
-
extractSubjectId
The method tries to extract the pattern id of the given text. If such an id is found, it is returned, otherwise
null
is returned.- Parameters:
text
- the given text- Returns:
- the id of the given text or null, if no id is found
-
extractOriginalPlainTextMessage
This method returns the first part of a given message that meets the following conditions:
- The part is unnamed
- The content type is text/plain
- Parameters:
p
- the message which is to scan, typically of typeMessage
, but the method works for any other objects that implement thePart
interface.- Returns:
- the part that meets the described conditions.
- Throws:
MessagingException
IOException
-
extractOriginalHTMLMessage
This method returns the first part of a given message that meets the following conditions:
- The part is unnamed
- The content type is text/html
- Parameters:
p
- the message which is to scan, typically of typeMessage
, but the method works for any other objects that implement thePart
interface.- Returns:
- the part that meets the described conditions.
- Throws:
MessagingException
IOException
-
extractAttachments
This method returns all parts of a given message that are not of type multipart/*. That means that also the mail body parts (text/plain and text/html) are returned.
- Parameters:
msg
- the message which is to scan- Returns:
- all parts of the message that are not multiparts
- Throws:
MessagingException
IOException
-
extractAttachmentsWithoutInlineImages
public static List<Part> extractAttachmentsWithoutInlineImages(Message msg) throws MessagingException, IOException This method returns all parts of a given message that are not of type multipart/*, but without inline images. Inline images have a "Content-Id" and in most cases the header attribute "Content-Disposition: inline". This method also returns mail body parts (text/plain and text/html).
- Parameters:
msg
- the message which is to scan- Returns:
- all parts of the message that are not multiparts and not an inline image
- Throws:
MessagingException
IOException
-
htmlBodyToPlainText
This method takes the content of a HTML body, i.e. everything between <body> and </body>, and removes the tags.
- Parameters:
strHtml
- the content of the HTML body- Returns:
- the given string but without tags
-
extractHtmlBodyContent
This method takes a HTML string and extracts the content of the body tag, i.e. everything between <body> and </body>.
- Parameters:
strHtml
- a HTML string- Returns:
- the content of the <body> tag.
-
getDisplayFilename
Returns the filename of a given part or "unnamed" if the part does not have a name.
- Parameters:
part
- the part- Returns:
- the filename of the given part.
- Throws:
Exception
-
getDisplayMailContent
public static String getDisplayMailContent(Message msg, boolean htmlPreferred, boolean onlyBody) throws Exception This method returns a HTML-encoded mail-content (not only the html-content between body tags)
The returned string can be displayed within every html file. Therefore the string does only contain html elements, that can be placed within the body-tag.
- Parameters:
msg
- the mail of which to extract the contenthtmlPreferred
- pass true if you prefer the text/html over the text/plain variant, otherwise false.onlyBody
- iftrue
, extract html-content between body tags only; otherwise the whole content (text/html or text/plain). this parameter is used, if parameter htmlPreferred istrue
and message contains a text/html part.- Returns:
- the HTML-encoded mail content
- Throws:
Exception
-
adjustImageTags
This method takes a HTML string that contains inline images (i.e. <img> tags with a
src
attribute that points to a part of the mail).
It alters thesrc
attributes of these <img> tags because in order to display inline images within the browser.- Parameters:
htmlString
- the original HTML string that contains inline imagesparts
- all parts of this message.- Returns:
- the new HTML string that contains the altered
src
attributes - Throws:
MessagingException
-
decodeText
Decodes the given encoded text (e.g. mail subjects). This method is an adapted version of the methodMimeUtility.decodeText(String)
, but with differences:- If parameter txt is empty, txt is returned
- In case of an error, the original text (parameter txt) is returned
- Parameters:
txt
- the text to decode- Returns:
- the decoded text, if given, or in case of an error the original text
-
htmlDecode
This method replaces entities with their real symbols, i.e.- < with <
- > with >
- " with "
- & with &
-   with blank
- Parameters:
content
-- Returns:
- string with real symbols instead of html entities
-
hasAttachments
Method to check, if the message has real attachments and inline images too, i.e. the mail body parts (text/plain and text/html) are not considered. -
hasAttachmentsWithoutInlineImages
Method to check, if the message has real attachments, i.e. the mail body parts (text/plain and text/html) and the inline images are not considered. -
getFrom
Return the sender information in message header (From or Sender) of given message. By default the methodgetFrom()
of message object itself is called, but in case of parsing errors the information is extracted manually as done if session propertymail.mime.address.strict
has been set to valuefalse
.- Parameters:
msg
- theMessage
object- Returns:
- the addresses that could be determined from message header
- Throws:
MessagingException
-
getDisplayAddressList
Returns a string containing a list of email addresses like <address-1>, <address-2>, ..., <address-n>,
- Parameters:
addresses
- the mail addresses- Returns:
- the string containing the list of addresses
-
getDisplayAddress
Returns a HTML encoded string that represents a given email address. The string looks like "someone@somewhere.com" or "Some One <someone@somewhere.com>", if the name of the person is specified.
- Parameters:
a
- the address- Returns:
- the string that represents the address.
-
getDisplayAddressList
Returns a string containing a list of email addresses like <address-1>, <address-2>, ..., <address-n>,
- Parameters:
msg
- theMessage
objectfield
- the needed address-field of the message- Returns:
- the string containing the list of addresses
- Throws:
MessagingException
-
isMailReceived
Method to check, if email contains received attribute(s).- Parameters:
msg
- theMessage
object- Returns:
true
, if "Received" was found in header, otherwisefalse
- Throws:
MessagingException
-