Package com.randomnoun.common.email
Class EmailWrapper
java.lang.Object
com.randomnoun.common.email.EmailWrapper
Provides a simple, one-class wrapper around the Java Mail API. Use the
emailTo(String, String, String, String, String, String, String)
method to send a mail in a single method call, or
emailToNoEx(String, String, String, String, String, String, String)
to send an email ignoring exceptions.
SMTP authentication is supported.
S/MIME encryption isn't supported, but would be relatively easy to add;
Use the (somewhat more complex) emailAttachmentTo(Map)
method to send emails
with attachments sourced from the file system, byte arrays or the classpath,
or to modify the email headers.
- Author:
- knoxg
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
emailAttachmentTo
(Map<Object, Object> params) Versatile extendable email wrapper method.static void
emailTo
(String to, String from, String host, String subject, String bodyText, String username, String password) A simpler version of emailTo.static void
emailTo
(String to, String from, String host, String subject, String bodyText, String bodyHtml, String username, String password) A simpler version of emailTo, with HTML.static void
emailToNoEx
(String to, String from, String host, String subject, String bodyText, String username, String password) A static method which provides the facility to easily send off an email using the JavaMail API.
-
Field Details
-
logger
-
-
Constructor Details
-
EmailWrapper
public EmailWrapper()
-
-
Method Details
-
emailToNoEx
public static void emailToNoEx(String to, String from, String host, String subject, String bodyText, String username, String password) A static method which provides the facility to easily send off an email using the JavaMail API. Mail-generated exceptions are sent to logger.error.- Parameters:
to
- A comma-separated list of recipientsfrom
- The address to place in the From: field of the emailsubject
- The subject textbodyText
- The message text
-
emailTo
public static void emailTo(String to, String from, String host, String subject, String bodyText, String username, String password) throws jakarta.mail.MessagingException A simpler version of emailTo.- Parameters:
to
- A comma-separated list of recipientsfrom
- The address to place in the From: field of the emailhost
- The SMTP host to usesubject
- The subject textbodyText
- The message textusername
- The SMTP user to send from (null if not authenticated)password
- The SMTP password to use (null if not authenticated)- Throws:
jakarta.mail.MessagingException
-
emailTo
public static void emailTo(String to, String from, String host, String subject, String bodyText, String bodyHtml, String username, String password) throws jakarta.mail.MessagingException A simpler version of emailTo, with HTML.- Parameters:
to
- A comma-separated list of recipientsfrom
- The address to place in the From: field of the emailhost
- The SMTP host to usesubject
- The subject textbodyText
- The message textbodyHtml
- The message text, in HTML formatusername
- The SMTP user to send from (null if not authenticated)password
- The SMTP password to use (null if not authenticated)- Throws:
jakarta.mail.MessagingException
-
emailAttachmentTo
public static void emailAttachmentTo(Map<Object, Object> params) throws jakarta.mail.MessagingExceptionVersatile extendable email wrapper method. This method takes a structured map as input, representing the message to be sent. It supports multiple to, from, cc, bcc, and replyto fields. It supports custom headers. It supports attachments from files on the filesystem, classpath resources, and passed in directly. The following attributes are accepted by this method- to - To addresses, comma-separated. Will also include any custom headers supplied with the headername of 'to'
- from - From address
- subject - The subject of the email
- bodyText - The body text of the email
- bodyHtml - The body text of the email, in HTML format
- username - If not null, the username to authenticate to the SMTP server
- password - If not null, the password with which to authenticate to the SMTP
- cc - CC addresses, comma-separated. Will also include any custom headers supplied with the headername of 'cc'
- bcc - BCC addresses, comma-separated. Will also include any custom headers supplied with the headername of 'bcc'
- replyTo - Reply-To addresses, comma-separated. Will also include any custom headers supplied with the headername of 'replyTo'
- client - email client to use in generated MessageID (defaults to "JavaMail")
- suffix - suffix to use in generated MessageID (defaults to "username@host") (both client & suffix must be specified together)
- sessionProperties - A map containing additional JavaMail session properties
- headers - A structured list of custom headers
- name - The header name
- value - The header value
- attachFiles Attachments to this email, sourced from the filesystem
- filename - The file on the local filesystem which contains the data to send
- attachFilename - The name of the file visible in the email
- contentType - The content-type to assign to this file. Will default to
application/octet-stream
- attachResources Attachments to this email, sourced from the classLoader
- resource - The resource name
- attachFilename - The name of the file visible in the email
- contentType - The content-type to assign to this file. Will default to
application/octet-stream
- classLoader - Any class which will indicate which class loader to use to find this resource, or a ClassLoader instance. If missing, will default to the class loader of the EmailWrapper class.
- attachData Attachments to this email, passed in directly
- data - The data comprising the attachment. Can be either a byte array, or a string. If the object is of any other type, then it is converted to a string using it's .toString() method.
- attachFilename - The name of the file visible in the email
- contentType - The content-type to assign to this file. Will default
to
application/octet-stream
- Throws:
jakarta.mail.MessagingException
- An error occurred sending the email
-