Class EmailSender
- java.lang.Object
-
- com.linkare.commons.communications.smtp.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 Summary
Fields Modifier and Type Field Description private static StringCONTENT_TYPEprivate static LoggerLogprivate static StringMAIL_CONTENT_TYPEprivate static StringMAIL_SENDER_MAX_RECIPIENTSprivate static intMAX_MAIL_RECIPIENTSprivate static javax.mail.Sessionsessionprivate static StringSMTP_PROPERTIES
-
Constructor Summary
Constructors Constructor Description EmailSender()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static voidaddAttachment(javax.mail.internet.MimeMultipart multipart, List<String> attachmentsList)Adds an attachment to the email based on the fileAttachment @paramprivate static voidaddRecipients(javax.mail.Message message, javax.mail.Message.RecipientType recipientType, Collection<String> emailAddresses, Collection<String> unsentMails)Adds the e-mail addresses to a list of receipientsprotected static StringconstructFromString(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.private static Stringencode(String string)static Collection<String>forward(javax.mail.internet.MimeMessage message, List<String> bccAddressesToforward)Forwards the e-mail message to the bccAddressesprivate static voidregisterInvalidAddresses(Collection<String> unsentAddresses, javax.mail.SendFailedException e, Collection<String> toAddresses, Collection<String> ccAddresses, Collection<String> bccAddresses)Register invalid e-mail addresses.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 inbuild.propertiesstatic Collection<String>send(String fromName, String fromAddress, String[] replyTos, Collection<String> toAddresses, Collection<String> ccAddresses, Collection<String> bccAddresses, String subject, String body, String contentType)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 sentstatic 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 inbuild.propertiesstatic voidsend(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.
-
-
-
Field Detail
-
MAIL_CONTENT_TYPE
private static final String MAIL_CONTENT_TYPE
- See Also:
- Constant Field Values
-
MAIL_SENDER_MAX_RECIPIENTS
private static final String MAIL_SENDER_MAX_RECIPIENTS
- See Also:
- Constant Field Values
-
SMTP_PROPERTIES
private static final String SMTP_PROPERTIES
- See Also:
- Constant Field Values
-
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
-
-
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 inbuild.properties- Parameters:
fromName-fromAddress-replyTos-toAddresses-ccAddresses-bccAddresses-subject-body-- Returns:
- a
Collectionof 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 inbuild.properties- Parameters:
fromName-fromAddress-replyTos-toAddresses-ccAddresses-bccAddresses-subject-body-attachments-- Returns:
- a
Collectionof 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.MessagingExceptionAdds 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)
- Parameters:
fromName-fromAddress-replyTos-toAddresses-ccAddresses-bccAddresses-subject-body-contentType-- Returns:
-
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
Collectionof 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 exceptiontoAddresses-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 sentfromAddress- 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.MessagingExceptionAdds the e-mail addresses to a list of receipients- Parameters:
message-recipientType-emailAddresses-unsentMails-- Throws:
javax.mail.MessagingException
-
-