Class MailUtil

java.lang.Object
com.groiss.util.MailUtil

public class MailUtil extends Object
Utility class containing methods for dealing with Message objects.
  • Field Details

  • Method Details

    • convertMessageToBytes

      public static byte[] convertMessageToBytes(Message msg)
      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

      public static String sanitizeCRLF(String s)
      Replace sequences of CRLF characters by single spaces. Usage recommended to sanitize the subject of mail messages, that is before calling Message.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

      public static Message convertBytesToMessage(byte[] msgContent)
      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

      public static Message convertBytesToMessage(byte[] msgContent, Session s)
      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

      public static String getSMTPHost()
      Get the standard SMTP-host defined in configuration
      Returns:
      the SMTP-host of configuration parameter "mail.smtphost" or null, if not defined.
    • getSMTPCredentials

      public static Pair<String,String> 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

      public static String getSMTPCommunicationType()
      Get configured communication type... possible values: PLAIN, STARTTLS, ENCRYPTED or TRUSTED
      Returns:
      the communication type defined in configuration parameter "ep.mail.smtp.communicationtype"
    • isSecureSMTP

      public static boolean isSecureSMTP(String type)
      Check, if type is a communication type of ENCRYPTED or TRUSTED
      Parameters:
      type - the communication type.
      Returns:
      true, if communication type is ENCRYPTED or TRUSTED; otherwise false
    • getSender

      public static String 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

      public static String[] convertToArray(String to)
      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

      public static String getMailSocketFactoryName(String communicationType)
      Get the socket factory name of given communication type
      Parameters:
      communicationType - string representation of the communication type: either ENCRYPTED or TRUSTED or any other string to use plain communication
      Returns:
      the socket factory which provides the given communcationType:
      • TRUSTED returns "com.groiss.mail.ssl.TrustedMailSSLSocketFactory"
      • ENCRYPTED returns "com.groiss.mail.ssl.EncryptedMailSSLSocketFactory"
      • in all other cases "javax.net.SocketFactory" is returned.
    • convertToMimeMessage

      public static MimeMessage convertToMimeMessage(org.apache.poi.hsmf.MAPIMessage msg) throws Exception

      Method to convert MAPIMessage to MimeMessage.

      Parameters:
      msg - the MAPIMessage object
      Returns:
      the converted MimeMessage object
      Throws:
      Exception
    • isValidEmailAddress

      public static boolean isValidEmailAddress(String email)
      Method to validate given email address.
      Parameters:
      email - the email address to validate
      Returns:
      true, if email is a correct email address
    • getMailSubjectPattern

      public static String 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

      public static String getMessageId(Message msg)
      This method returns the message-id of the given message
      Parameters:
      msg - the MimeMessage object
      Returns:
      the message-id of given message or null, if the id could not be determined
    • extractSender

      public static InternetAddress extractSender(Message msg) throws Exception

      Extracts the sender address of a given message.

      Parameters:
      msg - the given Message
      Returns:
      the sender address of the message
      Throws:
      Exception
    • extractSubjectId

      public static String extractSubjectId(String text)

      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

      public static Part extractOriginalPlainTextMessage(Part p) throws MessagingException, IOException

      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 type Message, but the method works for any other objects that implement the Part interface.
      Returns:
      the part that meets the described conditions.
      Throws:
      MessagingException
      IOException
    • extractOriginalHTMLMessage

      public static Part extractOriginalHTMLMessage(Part p) throws MessagingException, IOException

      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 type Message, but the method works for any other objects that implement the Part interface.
      Returns:
      the part that meets the described conditions.
      Throws:
      MessagingException
      IOException
    • extractAttachments

      public static List<Part> extractAttachments(Message msg) throws MessagingException, IOException

      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

      public 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.

      Parameters:
      strHtml - the content of the HTML body
      Returns:
      the given string but without tags
    • extractHtmlBodyContent

      public 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>.

      Parameters:
      strHtml - a HTML string
      Returns:
      the content of the <body> tag.
    • getDisplayFilename

      public static String getDisplayFilename(Part part) throws Exception

      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 content
      htmlPreferred - pass true if you prefer the text/html over the text/plain variant, otherwise false.
      onlyBody - if true, extract html-content between body tags only; otherwise the whole content (text/html or text/plain). this parameter is used, if parameter htmlPreferred is true and message contains a text/html part.
      Returns:
      the HTML-encoded mail content
      Throws:
      Exception
    • adjustImageTags

      public static String adjustImageTags(String htmlString, List<Part> parts) throws MessagingException

      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 the src attributes of these <img> tags because in order to display inline images within the browser.

      Parameters:
      htmlString - the original HTML string that contains inline images
      parts - all parts of this message.
      Returns:
      the new HTML string that contains the altered src attributes
      Throws:
      MessagingException
    • decodeText

      public static String decodeText(String txt)
      Decodes the given encoded text (e.g. mail subjects). This method is an adapted version of the method MimeUtility.decodeText(String), but with differences:
      1. If parameter txt is empty, txt is returned
      2. 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

      public static String htmlDecode(String content)
      This method replaces entities with their real symbols, i.e.
      • &lt; with <
      • &gt; with >
      • &quot; with "
      • &amp; with &
      • &nbsp with blank
      Parameters:
      content -
      Returns:
      string with real symbols instead of html entities
    • hasAttachments

      public static boolean hasAttachments(Message msg) throws Exception
      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.
      Parameters:
      msg - the Message object
      Returns:
      true, if the message contains attachments; otherwise false
      Throws:
      Exception
    • hasAttachmentsWithoutInlineImages

      public static boolean hasAttachmentsWithoutInlineImages(Message msg) throws Exception
      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.
      Parameters:
      msg - the Message object
      Returns:
      true, if the message contains attachments; otherwise false
      Throws:
      Exception
    • getFrom

      public static Address[] getFrom(Message msg) throws MessagingException
      Return the sender information in message header (From or Sender) of given message. By default the method getFrom() of message object itself is called, but in case of parsing errors the information is extracted manually as done if session property mail.mime.address.strict has been set to value false.
      Parameters:
      msg - the Message object
      Returns:
      the addresses that could be determined from message header
      Throws:
      MessagingException
    • getDisplayAddressList

      public static String getDisplayAddressList(Address[] addresses)

      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

      public static String getDisplayAddress(Address a)

      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

      public static String getDisplayAddressList(Message msg, String field) throws MessagingException

      Returns a string containing a list of email addresses like <address-1>, <address-2>, ..., <address-n>,

      Parameters:
      msg - the Message object
      field - the needed address-field of the message
      Returns:
      the string containing the list of addresses
      Throws:
      MessagingException
    • isMailReceived

      public static boolean isMailReceived(Message msg) throws MessagingException
      Method to check, if email contains received attribute(s).
      Parameters:
      msg - the Message object
      Returns:
      true, if "Received" was found in header, otherwise false
      Throws:
      MessagingException