Class EmailSender


  • public class EmailSender
    extends Object
    Used to send e-mails through an smtp protocol and using the javax.mail API. This class was inspired on the homonymous class of the fenix-tools project.
    Author:
    Ricardo EspĂ­rito Santo - Linkare TI
    • Field Detail

      • CONTENT_TYPE

        private static final String CONTENT_TYPE
      • MAX_MAIL_RECIPIENTS

        private static final int MAX_MAIL_RECIPIENTS
      • session

        private static final javax.mail.Session session
      • Log

        private static Logger Log
    • Constructor Detail

      • EmailSender

        public EmailSender()
    • Method Detail

      • forward

        public static Collection<String> forward​(javax.mail.internet.MimeMessage message,
                                                 List<String> bccAddressesToforward)
        Forwards the e-mail message to the bccAddresses
        Parameters:
        message -
        bccAddressesToforward -
        Returns:
        a list of skipped addresses
      • send

        public static Collection<String> send​(String fromName,
                                              String fromAddress,
                                              String[] replyTos,
                                              Collection<String> toAddresses,
                                              Collection<String> ccAddresses,
                                              Collection<String> bccAddresses,
                                              String subject,
                                              String body)
        A constructor whose content type is predefined with whatever value is set in build.properties
        Parameters:
        fromName -
        fromAddress -
        replyTos -
        toAddresses -
        ccAddresses -
        bccAddresses -
        subject -
        body -
        Returns:
        a Collection of addresses that never got reached
      • send

        public static Collection<String> send​(String fromName,
                                              String fromAddress,
                                              String[] replyTos,
                                              Collection<String> toAddresses,
                                              Collection<String> ccAddresses,
                                              Collection<String> bccAddresses,
                                              String subject,
                                              String body,
                                              List<String> attachments)
        A constructor whose content type is predefined with whatever value is set in build.properties
        Parameters:
        fromName -
        fromAddress -
        replyTos -
        toAddresses -
        ccAddresses -
        bccAddresses -
        subject -
        body -
        attachments -
        Returns:
        a Collection of addresses that never got reached
      • send

        public static void send​(String fromName,
                                String fromAddress,
                                String to,
                                String subject,
                                String body)
        A simpler form of sending an e-mail specify only the from the to the subject and the body No multiple lists no from name and the content type is given by the property file.
        Parameters:
        from -
        to -
        subject -
        body -
      • addAttachment

        private static void addAttachment​(javax.mail.internet.MimeMultipart multipart,
                                          List<String> attachmentsList)
                                   throws javax.mail.MessagingException
        Adds an attachment to the email based on the fileAttachment @param
        Parameters:
        multipart -
        fileAttachment -
        Throws:
        javax.mail.MessagingException
      • send

        public static Collection<String> send​(String fromName,
                                              String fromAddress,
                                              String[] replyTos,
                                              Collection<String> toAddresses,
                                              Collection<String> ccAddresses,
                                              Collection<String> bccAddresses,
                                              String subject,
                                              String body,
                                              String contentType,
                                              List<String> attachmentsList)
        A constructor that specifies the content type of the message to be sent
        Parameters:
        fromName -
        fromAddress -
        replyTos -
        toAddresses -
        ccAddresses -
        bccAddresses -
        subject -
        body -
        contentType -
        attachmentsList -
        Returns:
        a Collection of addresses that never got reached
      • registerInvalidAddresses

        private static void registerInvalidAddresses​(Collection<String> unsentAddresses,
                                                     javax.mail.SendFailedException e,
                                                     Collection<String> toAddresses,
                                                     Collection<String> ccAddresses,
                                                     Collection<String> bccAddresses)
        Register invalid e-mail addresses. These addresses have been attempted to reach at least once and failed
        Parameters:
        unsentAddresses -
        e - the exception
        toAddresses -
        ccAddresses -
        bccAddresses -
      • constructFromString

        protected static String constructFromString​(String fromName,
                                                    String fromAddress)
        Builds the from address to display on the e-mail that gets sent if we have a from name we display it else we only display the addess. So it either becomes: "John Doe " OR john@doe.com if no name is provided
        Parameters:
        fromName - The name on who's behalf is this message sent
        fromAddress - The e-mail address from which this message is sent
        Returns:
        the "from" field full composed text
      • addRecipients

        private static void addRecipients​(javax.mail.Message message,
                                          javax.mail.Message.RecipientType recipientType,
                                          Collection<String> emailAddresses,
                                          Collection<String> unsentMails)
                                   throws javax.mail.MessagingException
        Adds the e-mail addresses to a list of receipients
        Parameters:
        message -
        recipientType -
        emailAddresses -
        unsentMails -
        Throws:
        javax.mail.MessagingException