Class MailUtil


  • public final class MailUtil
    extends java.lang.Object
    This class provides a set of static methods which perform various utility operations for JavaMail API.
    • sendMail(...) methods send one and only one mail for all recipients.
    • sendMails(...) methods send one mail per recipient (spliting the recipients on comma).
    • sendMessage(...) methods send one mail per given Message.
    Version:
    $Revision: 136265 $
    Author:
    Olivier Dedieu <olivier.dedieu@jalios.com>, Olivier Jaquemet
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static javax.mail.Message buildMessage​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String[] bodyPart, java.lang.String[] bodyPartEncoding, java.util.Map<java.io.File,​java.lang.String> files, int priority)
      Build a MimeMessage bodyPartEncoding values are: text/plain; charset=UTF-8 | text/html; charset=UTF-8
      static javax.mail.Message buildMessage​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String[] bodyPart, java.lang.String[] bodyPartEncoding, java.util.Map<java.io.File,​java.lang.String> files, int priority, javax.mail.BodyPart[] bodyPartObjects)
      Build a MimeMessage bodyPartEncoding values are: text/plain; charset=UTF-8 | text/html; charset=UTF-8
      static boolean checkEmail​(java.lang.String email)
      Checks that the given string is not empty and has a valid email syntax.
      static boolean checkEmailLocalPart​(java.lang.String localPart)
      Checks a Mail local part using checkEmail(String).
      static void configure​(java.lang.String host, java.lang.String port, java.lang.String login, java.lang.String password, java.util.Properties addedProps)
      Configure MailUtil properties (of javax.mail.Session) using the given values.
      static void disable()
      Disable MailUtil, that is all call to methods of MailUtil will results in log message and no mail is ever send.
      static java.util.Set<java.lang.String> emailsStringToEmailSet​(java.lang.String emails)
      Retrieve a Set of emails from a string of coma separated emails
      static void enable()
      Enable MailUtil, that is all call to methods of MailUtil will send mail using the configuration given during call to configure(String, String, String, String, Properties).
      static java.lang.String getBeautifiedEmail​(java.lang.String email)
      Convert a text email to an HTML representation.
      static java.lang.String getBeautifiedEmail​(java.lang.String email, boolean isColored)
      Convert a text email to an HTML representation.
      static java.lang.String getBeautifiedEmail​(java.lang.String email, java.lang.String css1, java.lang.String css2, boolean isColored)
      Convert an email to an HTML representation.
      static java.lang.String getCanonizedEmail​(java.lang.String email)
      Canonizes an email.
      static java.lang.String getCharset​(java.lang.String contentType)
      Returns charset for the given mail contentType
      static javax.mail.internet.InternetAddress getIA​(java.lang.String email)
      Retrieve a new InternetAddress instance (with properly encoded personnal).
      static java.util.Set<javax.mail.internet.InternetAddress> getIAFromMailSet​(java.util.Set<java.lang.String> mailSet)
      Returns a Set of InternetAddress from a Set of mail String
      static javax.mail.internet.InternetAddress getIAQuietly​(java.lang.String address)
      Returns an InternetAddress object from the specified email address
      static javax.mail.internet.InternetAddress getInternetAddress​(java.lang.String email)
      Retrieve a new InternetAddress instance (with properly encoded personnal).
      static int getPriority​(java.lang.String priority)
      Returns the given X-Priority String as int.
      This method handles comment suffix like: "1 (Highest)"
      static int getPriority​(javax.mail.Message msg)
      Returns the given Message's X-Priority header as int.
      This method handles comment suffix like: "1 (Highest)"
      static java.util.TreeSet<java.lang.String> getRecipientSet​(java.util.Collection<javax.mail.Message> messages, javax.mail.Message.RecipientType type)
      Utility method which create a TreeSet of all recipients email from the specified messages.
      static javax.mail.Session getSession()
      Retrieve the sesssion being used to send mails.
      static boolean hasRecipient​(javax.mail.Message message)
      Check if the specified message has recipient.
      static java.lang.String logAddress​(javax.mail.Address[] address)  
      static java.lang.String logFlags​(javax.mail.Flags.Flag[] flags)  
      static java.lang.String logHeaders​(javax.mail.Message msg)  
      static java.lang.String logMessage​(javax.mail.Message msg)  
      static void sendMail​(java.lang.String from, java.lang.String recipients, java.lang.String subject, java.lang.String bodyText)
      Send 1 Mail to all the given recipients.
      static void sendMail​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String[] bodyPart, java.lang.String[] bodyPartEncoding, java.util.Map<java.io.File,​java.lang.String> files, int priority, java.lang.String host, int port, java.lang.String login, java.lang.String password)
      Send 1 Mail to all the given recipients.
      static void sendMail​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String[] bodyPart, java.lang.String[] bodyPartEncoding, java.util.Map<java.io.File,​java.lang.String> files, int priority, javax.mail.BodyPart[] bodyPartObjects, java.lang.String host, int port, java.lang.String login, java.lang.String password)
      Send 1 Mail to all the given recipients.
      static void sendMail​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String bodyText, java.util.Map<java.io.File,​java.lang.String> files, int priority)
      Send 1 Mail to all the given recipients.
      static void sendMail​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String bodyText, java.util.Map<java.io.File,​java.lang.String> files, int priority, java.lang.String host, int port, java.lang.String login, java.lang.String password)
      Send 1 Mail to all the given recipients.
      static void sendMail​(java.lang.String from, java.lang.String recipients, java.lang.String subject, java.lang.String bodyText, java.util.Map<java.io.File,​java.lang.String> files, int priority)
      Send 1 Mail to all the given recipients.
      static java.util.List<java.lang.String> sendMails​(java.lang.String from, java.lang.String recipients, java.lang.String subject, java.lang.String bodyText)
      Send 1 mail for each given recipients.
      static java.util.List<java.lang.String> sendMails​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String[] bodyPart, java.lang.String[] bodyPartEncoding, java.util.Map<java.io.File,​java.lang.String> files, int priority, java.lang.String host, int port, java.lang.String login, java.lang.String password)
      Send n Mails.
      static java.util.List<java.lang.String> sendMails​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String[] bodyPart, java.lang.String[] bodyPartEncoding, java.util.Map<java.io.File,​java.lang.String> files, int priority, javax.mail.BodyPart[] bodyPartObjects, java.lang.String host, int port, java.lang.String login, java.lang.String password)
      Send n Mails.
      static java.util.List<java.lang.String> sendMails​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String bodyText, java.util.Map<java.io.File,​java.lang.String> files, int priority)
      Send n Mails.
      static java.util.List<java.lang.String> sendMails​(java.lang.String from, java.lang.String recipients, java.lang.String copies, java.lang.String bcopies, java.lang.String subject, java.lang.String bodyText, java.util.Map<java.io.File,​java.lang.String> files, int priority, java.lang.String host, int port, java.lang.String login, java.lang.String password)
      Send n Mails.
      static java.util.List<java.lang.String> sendMails​(java.lang.String from, java.lang.String recipients, java.lang.String subject, java.lang.String bodyText, java.util.Map<java.io.File,​java.lang.String> files, int priority)
      Send 1 mail for each given recipients.
      static java.util.List<javax.mail.Message> sendMessage​(javax.mail.Message msg, boolean catchMailErrors)
      Send given message.
      static java.util.List<javax.mail.Message> sendMessage​(javax.mail.Message msg, java.lang.String host, int port, java.lang.String login, java.lang.String password, boolean catchMailErrors)
      Send the given message with the given smtp parameters.
      static java.util.List<javax.mail.Message> sendMessages​(javax.mail.Message[] msg, boolean catchMailErrors)
      Send given messages.
      static java.util.List<javax.mail.Message> sendMessages​(javax.mail.Message[] msg, java.lang.String host, int port, java.lang.String login, java.lang.String password, boolean catchMailErrors)
      Send given messages.
      static void setEmailValidator​(EmailValidator validator)
      Sets the email validator to use.
      static java.lang.String stripQuoteReplies​(java.lang.String message, java.lang.String separator, java.lang.String reply)
      Strip message to wrap inline quotation by [quote] and remove other quotes and signatures
      static java.lang.String toggleReplies​(java.lang.String message, java.lang.String quote, java.lang.String label)
      Strip message to show/hide quotation using javascript and css.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BODYPART_TEXT_PLAIN_ENCODING

        public static final java.lang.String BODYPART_TEXT_PLAIN_ENCODING
        See Also:
        Constant Field Values
      • BODYPART_TEXT_HTML_ENCODING

        public static final java.lang.String BODYPART_TEXT_HTML_ENCODING
        See Also:
        Constant Field Values
      • BODYPART_ENCODINGS

        public static final java.lang.String[] BODYPART_ENCODINGS
        Predefined array of body part encoding.
        • First part is MailUtil.BODYPART_TEXT_PLAIN_ENCODING.
        • Second part is MailUtil.BODYPART_TEXT_HTML_ENCODING.
      • props

        protected static final java.util.Properties props
      • session

        protected static final javax.mail.Session session
      • enabled

        protected static boolean enabled
      • EMAIL_VALIDATOR

        protected static volatile EmailValidator EMAIL_VALIDATOR
    • Method Detail

      • configure

        public static void configure​(java.lang.String host,
                                     java.lang.String port,
                                     java.lang.String login,
                                     java.lang.String password,
                                     java.util.Properties addedProps)
        Configure MailUtil properties (of javax.mail.Session) using the given values.
        Parameters:
        host - host of the SMTP server
        port - port of the SMTP server
        login - login used to connect to the SMTP server
        password - password used to connect to the SMTP server
        addedProps - more properties that will be added to javax.mail.Session properties if not null.
      • disable

        public static void disable()
        Disable MailUtil, that is all call to methods of MailUtil will results in log message and no mail is ever send.
      • getSession

        public static javax.mail.Session getSession()
        Retrieve the sesssion being used to send mails.
        Returns:
        a configured Session object
      • getIAFromMailSet

        public static java.util.Set<javax.mail.internet.InternetAddress> getIAFromMailSet​(java.util.Set<java.lang.String> mailSet)
                                                                                   throws javax.mail.MessagingException
        Returns a Set of InternetAddress from a Set of mail String
        Parameters:
        mailSet - Set of mail String
        Returns:
        Set of InternetAddress
        Throws:
        javax.mail.MessagingException
      • getIAQuietly

        public static javax.mail.internet.InternetAddress getIAQuietly​(java.lang.String address)
        Returns an InternetAddress object from the specified email address
        Parameters:
        address - the email address. (eg: "John Smith" <john.smith@example.com>)
        Returns:
        InternetAddress
      • getIA

        public static javax.mail.internet.InternetAddress getIA​(java.lang.String email)
                                                         throws javax.mail.internet.AddressException
        Retrieve a new InternetAddress instance (with properly encoded personnal).
        Parameters:
        email - the email address. (eg: "John Smith" <john.smith@example.com>)
        Returns:
        a new InternetAddress object
        Throws:
        javax.mail.internet.AddressException - if the email address could not be parsed.
        Since:
        jcms-5.7.6, jcms-6.1.2, jcms-6.2
      • getInternetAddress

        public static javax.mail.internet.InternetAddress getInternetAddress​(java.lang.String email)
                                                                      throws javax.mail.internet.AddressException
        Retrieve a new InternetAddress instance (with properly encoded personnal).
        Parameters:
        email - the email address. (eg: "John Smith" <john.smith@example.com>)
        Returns:
        a new InternetAddress object
        Throws:
        javax.mail.internet.AddressException - if the email address could not be parsed.
        Since:
        jcms-5.7.6, jcms-6.1.2, jcms-6.2
      • checkEmail

        public static boolean checkEmail​(java.lang.String email)
        Checks that the given string is not empty and has a valid email syntax.
        Parameters:
        email - the email to be checked
        Returns:
        true if the email is valid, false otherwise
      • checkEmailLocalPart

        public static boolean checkEmailLocalPart​(java.lang.String localPart)
        Checks a Mail local part using checkEmail(String).
        Parameters:
        localPart - the local part to test its validity.
        Returns:
        true if checkEmail(localPart + "@example.com") is valid.
        Since:
        jcms-9.0.0
        See Also:
        checkEmail(String)
      • getCanonizedEmail

        public static java.lang.String getCanonizedEmail​(java.lang.String email)
        Canonizes an email. Example, returns olivier.dedieu@inria.fr for Olivier Dedieu <olivier.dedieu@inria.fr>
        Parameters:
        email - the email to be canonized
        Returns:
        the canonized email
      • getPriority

        public static int getPriority​(javax.mail.Message msg)
        Returns the given Message's X-Priority header as int.
        This method handles comment suffix like: "1 (Highest)"

        If no valid priority does match, returns PRIORITY_NORMAL by default.
        A valid priority is in range [1, 5], as the following:

        • 1 (Highest)
        • 2 (High)
        • 3 (Normal)
        • 4 (Low)
        • 5 (Lowest)
        Parameters:
        msg - the message to retrieve its X-Priority header.
        Returns:
        the X-Priority from 1 (Highest) to 5 (Lowest)
        Since:
        jcms-7.1.4, jcms-8.0.3, jcms-9.0.0
        See Also:
        getPriority(String)
      • getPriority

        public static int getPriority​(java.lang.String priority)
        Returns the given X-Priority String as int.
        This method handles comment suffix like: "1 (Highest)"

        If no valid priority does match, returns PRIORITY_NORMAL by default.
        A valid priority is in range [1, 5], as the following:

        • 1 (Highest)
        • 2 (High)
        • 3 (Normal)
        • 4 (Low)
        • 5 (Lowest)
        Parameters:
        priority - the priority as String
        Returns:
        the X-Priority from 1 (Highest) to 5 (Lowest)
        Since:
        jcms-7.1.4, jcms-8.0.3, jcms-9.0.0
        See Also:
        getPriority(Message)
      • getRecipientSet

        public static java.util.TreeSet<java.lang.String> getRecipientSet​(java.util.Collection<javax.mail.Message> messages,
                                                                          javax.mail.Message.RecipientType type)
        Utility method which create a TreeSet of all recipients email from the specified messages.
        Parameters:
        messages - the message in which to retrieve recipients
        type - the type of recipient to find, if null, all recipient are used
        Returns:
        a Set of email, never return null.
        Since:
        jcms-6.1
      • emailsStringToEmailSet

        public static java.util.Set<java.lang.String> emailsStringToEmailSet​(java.lang.String emails)
        Retrieve a Set of emails from a string of coma separated emails
        Parameters:
        emails - a string containing one or several mail addresses seperated with coma, e.g "Foo Bar" <foobar@company.com>, "John Smith" <js@company.com>
        Returns:
        a Set of unique email address, never return null.
      • getCharset

        public static java.lang.String getCharset​(java.lang.String contentType)
        Returns charset for the given mail contentType
        Parameters:
        contentType - the contentType header
        Returns:
        String decoded charset
      • getBeautifiedEmail

        public static java.lang.String getBeautifiedEmail​(java.lang.String email)
        Convert a text email to an HTML representation.

        Invokes getBeautifiedEmail(String, String, String, boolean) with css1="label" and css2="email" and no color.

        Parameters:
        email - the email String representation
        Returns:
        String HTML representation
      • getBeautifiedEmail

        public static java.lang.String getBeautifiedEmail​(java.lang.String email,
                                                          boolean isColored)
        Convert a text email to an HTML representation.

        Invokes getBeautifiedEmail(String, String, String, boolean) with css1="label" and css2="email".

        Parameters:
        email - the email String representation
        isColored - will set a color
        Returns:
        String HTML representation
      • getBeautifiedEmail

        public static java.lang.String getBeautifiedEmail​(java.lang.String email,
                                                          java.lang.String css1,
                                                          java.lang.String css2,
                                                          boolean isColored)
        Convert an email to an HTML representation.
        • Given string ""FirstName LastName" <first.last@company.com>"
          output is :
          <span class="css1">"FirstName LastName"</span> <span class="css2">&lt;first.last@company.com>&lt;/span>
        • Given string "FirstName LastName <first.last@company.com>"
          output is :
          <span class="css1">FirstName LastName</span> <span class="css2">&lt;first.last@company.com>&lt;/span>
        • Given string "first.last@company.com"
          output is :
          <span class="css1">first.last@company.com</span>
        If label is missing then use email instead
        Parameters:
        email - the email String representation
        css1 - the first css to apply
        css2 - the second css to apply
        isColored - true to add a style attribute to the first span, with a text color computed from the email value
        Returns:
        String HTML representation
      • toggleReplies

        public static java.lang.String toggleReplies​(java.lang.String message,
                                                     java.lang.String quote,
                                                     java.lang.String label)
        Strip message to show/hide quotation using javascript and css. A link is added and quotation is wrapped into a <cite> tag
        Parameters:
        message - the text message
        quote - the line starts
        label - the link label
        Returns:
        the modified String
      • stripQuoteReplies

        public static java.lang.String stripQuoteReplies​(java.lang.String message,
                                                         java.lang.String separator,
                                                         java.lang.String reply)
        Strip message to wrap inline quotation by [quote] and remove other quotes and signatures
        Parameters:
        message - the text message
        separator - the line separator of signature
        reply - a string to identify begining of reply (can be null)
        Returns:
        the message cleaned
      • hasRecipient

        public static boolean hasRecipient​(javax.mail.Message message)
        Check if the specified message has recipient.
        Parameters:
        message - the message to check
        Returns:
        true if the message has at least one recipient (TO, CC or BCC), false otherwise and if the message is null
        Since:
        jcms-6.2, jcms-6.1.3
      • sendMail

        public static void sendMail​(java.lang.String from,
                                    java.lang.String recipients,
                                    java.lang.String subject,
                                    java.lang.String bodyText)
                             throws javax.mail.NoSuchProviderException,
                                    javax.mail.MessagingException
        Send 1 Mail to all the given recipients.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMail

        public static void sendMail​(java.lang.String from,
                                    java.lang.String recipients,
                                    java.lang.String subject,
                                    java.lang.String bodyText,
                                    java.util.Map<java.io.File,​java.lang.String> files,
                                    int priority)
                             throws javax.mail.NoSuchProviderException,
                                    javax.mail.MessagingException
        Send 1 Mail to all the given recipients.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMail

        public static void sendMail​(java.lang.String from,
                                    java.lang.String recipients,
                                    java.lang.String copies,
                                    java.lang.String bcopies,
                                    java.lang.String subject,
                                    java.lang.String bodyText,
                                    java.util.Map<java.io.File,​java.lang.String> files,
                                    int priority)
                             throws javax.mail.NoSuchProviderException,
                                    javax.mail.MessagingException
        Send 1 Mail to all the given recipients.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMail

        public static void sendMail​(java.lang.String from,
                                    java.lang.String recipients,
                                    java.lang.String copies,
                                    java.lang.String bcopies,
                                    java.lang.String subject,
                                    java.lang.String bodyText,
                                    java.util.Map<java.io.File,​java.lang.String> files,
                                    int priority,
                                    java.lang.String host,
                                    int port,
                                    java.lang.String login,
                                    java.lang.String password)
                             throws javax.mail.NoSuchProviderException,
                                    javax.mail.MessagingException
        Send 1 Mail to all the given recipients.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        host - smtp server host (null means default smtp)
        port - smtp server port (<= 0 means default port)
        login - login to smtp server (null means default login)
        password - password to smtp server (null means default password)
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMail

        public static void sendMail​(java.lang.String from,
                                    java.lang.String recipients,
                                    java.lang.String copies,
                                    java.lang.String bcopies,
                                    java.lang.String subject,
                                    java.lang.String[] bodyPart,
                                    java.lang.String[] bodyPartEncoding,
                                    java.util.Map<java.io.File,​java.lang.String> files,
                                    int priority,
                                    java.lang.String host,
                                    int port,
                                    java.lang.String login,
                                    java.lang.String password)
                             throws javax.mail.NoSuchProviderException,
                                    javax.mail.MessagingException
        Send 1 Mail to all the given recipients.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyPart - the body parts to be included in the mail (text, html.. etc)
        bodyPartEncoding - the encodings associated to each body part (must be present for body part to be used)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        host - smtp server host (null means default smtp)
        port - smtp server port (<= 0 means default port)
        login - login to smtp server (null means default login)
        password - password to smtp server (null means default password)
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMail

        public static void sendMail​(java.lang.String from,
                                    java.lang.String recipients,
                                    java.lang.String copies,
                                    java.lang.String bcopies,
                                    java.lang.String subject,
                                    java.lang.String[] bodyPart,
                                    java.lang.String[] bodyPartEncoding,
                                    java.util.Map<java.io.File,​java.lang.String> files,
                                    int priority,
                                    javax.mail.BodyPart[] bodyPartObjects,
                                    java.lang.String host,
                                    int port,
                                    java.lang.String login,
                                    java.lang.String password)
                             throws javax.mail.NoSuchProviderException,
                                    javax.mail.MessagingException
        Send 1 Mail to all the given recipients.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyPart - the body parts to be included in the mail (text, html.. etc)
        bodyPartEncoding - the encodings associated to each body part (must be present for body part to be used)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        bodyPartObjects - array of BodyPart to included in the mail
        host - smtp server host (null means default smtp)
        port - smtp server port (<= 0 means default port)
        login - login to smtp server (null means default login)
        password - password to smtp server (null means default password)
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMails

        public static java.util.List<java.lang.String> sendMails​(java.lang.String from,
                                                                 java.lang.String recipients,
                                                                 java.lang.String subject,
                                                                 java.lang.String bodyText)
        Send 1 mail for each given recipients.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        Returns:
        a list of recipient String for which an exception was thrown during sending of mails
      • sendMails

        public static java.util.List<java.lang.String> sendMails​(java.lang.String from,
                                                                 java.lang.String recipients,
                                                                 java.lang.String subject,
                                                                 java.lang.String bodyText,
                                                                 java.util.Map<java.io.File,​java.lang.String> files,
                                                                 int priority)
        Send 1 mail for each given recipients. This is a shorter Method using basic parameters. All other parameters are guest from properties. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not faild and return anything when the connection to the smtp server cannot be established.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        Returns:
        a list of recipient String for which an exception was thrown during sending of mails
      • sendMails

        public static java.util.List<java.lang.String> sendMails​(java.lang.String from,
                                                                 java.lang.String recipients,
                                                                 java.lang.String copies,
                                                                 java.lang.String bcopies,
                                                                 java.lang.String subject,
                                                                 java.lang.String bodyText,
                                                                 java.util.Map<java.io.File,​java.lang.String> files,
                                                                 int priority)
        Send n Mails. Do not throw exceptions. This is a shorter Method using basic parameters. All other parameters are guest from properties. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not faild and return anything when the connection to the smtp server cannot be established.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        Returns:
        a list of recipient String that throws exceptions during sending of mails
      • sendMails

        public static java.util.List<java.lang.String> sendMails​(java.lang.String from,
                                                                 java.lang.String recipients,
                                                                 java.lang.String copies,
                                                                 java.lang.String bcopies,
                                                                 java.lang.String subject,
                                                                 java.lang.String bodyText,
                                                                 java.util.Map<java.io.File,​java.lang.String> files,
                                                                 int priority,
                                                                 java.lang.String host,
                                                                 int port,
                                                                 java.lang.String login,
                                                                 java.lang.String password)
        Send n Mails. Do not throw exceptions. This is a shorter Method using basic parameters. All other parameters are guest from properties. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not faild and return anything when the connection to the smtp server cannot be established.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyText - the body of the mail (plain text)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        host - not used
        port - not used
        login - not used
        password - not used
        Returns:
        a list of recipient String that throws exceptions during sending of mails
      • sendMails

        public static java.util.List<java.lang.String> sendMails​(java.lang.String from,
                                                                 java.lang.String recipients,
                                                                 java.lang.String copies,
                                                                 java.lang.String bcopies,
                                                                 java.lang.String subject,
                                                                 java.lang.String[] bodyPart,
                                                                 java.lang.String[] bodyPartEncoding,
                                                                 java.util.Map<java.io.File,​java.lang.String> files,
                                                                 int priority,
                                                                 java.lang.String host,
                                                                 int port,
                                                                 java.lang.String login,
                                                                 java.lang.String password)
        Send n Mails. Do not throw exceptions. This is the most complete Method using basic parameters. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not faild and return anything when the connection to the smtp server cannot be established.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyPart - the body parts to be included in the mail (text, html.. etc)
        bodyPartEncoding - the encodings associated to each body part (must be present for body part to be used)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        host - not used
        port - not used
        login - not used
        password - not used
        Returns:
        a list of recipient String that throws exceptions during sending of mails
      • sendMails

        public static java.util.List<java.lang.String> sendMails​(java.lang.String from,
                                                                 java.lang.String recipients,
                                                                 java.lang.String copies,
                                                                 java.lang.String bcopies,
                                                                 java.lang.String subject,
                                                                 java.lang.String[] bodyPart,
                                                                 java.lang.String[] bodyPartEncoding,
                                                                 java.util.Map<java.io.File,​java.lang.String> files,
                                                                 int priority,
                                                                 javax.mail.BodyPart[] bodyPartObjects,
                                                                 java.lang.String host,
                                                                 int port,
                                                                 java.lang.String login,
                                                                 java.lang.String password)
        Send n Mails. Do not throw exceptions. This is the most complete Method using basic parameters. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not faild and return anything when the connection to the smtp server cannot be established.
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blind carbon copy separated by a comma
        subject - the subject of the mail
        bodyPart - the body parts to be included in the mail (text, html.. etc)
        bodyPartEncoding - the encodings associated to each body part (must be present for body part to be used)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the mail to be sent, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        bodyPartObjects - array of BodyPart to included in the mail
        host - not used
        port - not used
        login - not used
        password - not used
        Returns:
        a list of recipient String that throws exceptions during sending of mails
      • sendMessage

        public static java.util.List<javax.mail.Message> sendMessage​(javax.mail.Message msg,
                                                                     boolean catchMailErrors)
                                                              throws javax.mail.NoSuchProviderException,
                                                                     javax.mail.MessagingException
        Send given message.
        Parameters:
        msg - Message to be sent
        catchMailErrors - set to true to prevent this method to throw exception when sending messages
        Returns:
        null if catchMailErrors is false, a List of Message that could not be sent if catchMailErrors is true
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMessages

        public static java.util.List<javax.mail.Message> sendMessages​(javax.mail.Message[] msg,
                                                                      boolean catchMailErrors)
                                                               throws javax.mail.NoSuchProviderException,
                                                                      javax.mail.MessagingException
        Send given messages.
        Parameters:
        msg - array of Message to be sent
        catchMailErrors - set to true to prevent this method to throw exception when sending messages
        Returns:
        null if catchMailErrors is false, a List of Message that could not be sent if catchMailErrors is true
        Throws:
        javax.mail.NoSuchProviderException
        javax.mail.MessagingException
      • sendMessage

        public static java.util.List<javax.mail.Message> sendMessage​(javax.mail.Message msg,
                                                                     java.lang.String host,
                                                                     int port,
                                                                     java.lang.String login,
                                                                     java.lang.String password,
                                                                     boolean catchMailErrors)
                                                              throws javax.mail.NoSuchProviderException,
                                                                     javax.mail.MessagingException
        Send the given message with the given smtp parameters.
        Parameters:
        msg - Message to be sent
        host - smtp server host (null means default smtp)
        port - smtp server port (<= 0 means default port)
        login - login to smtp server (null means default login)
        password - password to smtp server (null means default password)
        catchMailErrors - set to true to prevent this method to throw exception when sending messages
        Returns:
        null if catchMailErrors is false, a List of Message that could not be sent if catchMailErrors is true
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • sendMessages

        public static java.util.List<javax.mail.Message> sendMessages​(javax.mail.Message[] msg,
                                                                      java.lang.String host,
                                                                      int port,
                                                                      java.lang.String login,
                                                                      java.lang.String password,
                                                                      boolean catchMailErrors)
                                                               throws javax.mail.NoSuchProviderException,
                                                                      javax.mail.MessagingException
        Send given messages.
        Parameters:
        msg - array of Message to be sent
        host - smtp server host (null means default smtp)
        port - smtp server port (<= 0 means default port)
        login - login to smtp server (null means default login)
        password - password to smtp server (null means default password)
        catchMailErrors - set to true to prevent this method to throw exception when sending messages
        Returns:
        null if catchMailErrors is false, a List of Message that could not be sent if catchMailErrors is true
        Throws:
        javax.mail.NoSuchProviderException - if no SMTP provider (Transport) could be created
        javax.mail.MessagingException
      • logMessage

        public static java.lang.String logMessage​(javax.mail.Message msg)
                                           throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • logAddress

        public static java.lang.String logAddress​(javax.mail.Address[] address)
                                           throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • logFlags

        public static java.lang.String logFlags​(javax.mail.Flags.Flag[] flags)
                                         throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • logHeaders

        public static java.lang.String logHeaders​(javax.mail.Message msg)
                                           throws javax.mail.MessagingException
        Throws:
        javax.mail.MessagingException
      • buildMessage

        public static javax.mail.Message buildMessage​(java.lang.String from,
                                                      java.lang.String recipients,
                                                      java.lang.String copies,
                                                      java.lang.String bcopies,
                                                      java.lang.String subject,
                                                      java.lang.String[] bodyPart,
                                                      java.lang.String[] bodyPartEncoding,
                                                      java.util.Map<java.io.File,​java.lang.String> files,
                                                      int priority)
                                               throws javax.mail.MessagingException
        Build a MimeMessage bodyPartEncoding values are: text/plain; charset=UTF-8 | text/html; charset=UTF-8
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blank carbon copy separated by a comma
        subject - the subject of the mail
        bodyPart - the body parts to be included in the mail (text, html.. etc)
        bodyPartEncoding - the encodings associated to each body part (must be present for body part to be used)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the message to be built, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        Returns:
        a new Message instance
        Throws:
        javax.mail.MessagingException - When including attachments with your messages, if your program is a servlet, your users must upload the attachment besides tell you where to send the message. Uploading each file can be handled with a form encoding type of multipart/form-data.
      • buildMessage

        public static javax.mail.Message buildMessage​(java.lang.String from,
                                                      java.lang.String recipients,
                                                      java.lang.String copies,
                                                      java.lang.String bcopies,
                                                      java.lang.String subject,
                                                      java.lang.String[] bodyPart,
                                                      java.lang.String[] bodyPartEncoding,
                                                      java.util.Map<java.io.File,​java.lang.String> files,
                                                      int priority,
                                                      javax.mail.BodyPart[] bodyPartObjects)
                                               throws javax.mail.MessagingException
        Build a MimeMessage bodyPartEncoding values are: text/plain; charset=UTF-8 | text/html; charset=UTF-8
        Parameters:
        from - the address sender
        recipients - the list of recipients separated by a comma
        copies - the list of recipients in carbon copy separated by a comma
        bcopies - the list of recipients in blank carbon copy separated by a comma
        subject - the subject of the mail
        bodyPart - the body parts to be included in the mail (text, html.. etc)
        bodyPartEncoding - the encodings associated to each body part (must be present for body part to be used)
        files - a Map containing files to be attached to the mail. File as the key and an optional String identifier as the value (value is optional, if left null, an id is automatically generated from the file name)
        priority - priority of the message to be built, cf MailUtil.PRIORITY_*, ignored if 0 or any invalid value.
        bodyPartObjects - array of BodyPart to included in the mail
        Returns:
        a new Message instance
        Throws:
        javax.mail.MessagingException - When including attachments with your messages, if your program is a servlet, your users must upload the attachment besides tell you where to send the message. Uploading each file can be handled with a form encoding type of multipart/form-data.