Class MailMessage
- All Implemented Interfaces:
com.isomorphic.base.IAutoConfigurable
- Direct Known Subclasses:
TemplatedMailMessage
There are two general approaches to doing this. Here is the first one:
MailMessage message = new MailMessage();
message.setFrom("me@my.address");
message.addRecipients("friend1@his.address, friend2@her.address");
message.setSubject("Greetings");
message.setBody("Hello everyone!");
message.send();
Here is the second approach:
MailMessage message = new MailMessage();
message.parse("From: me@my.address\n"
+ "To: friend1@his.address, friend2@her.address\n"
+ "Subject: Greetings\n"
+ "Hello everyone!\n");
message.send();
You may mix and match the two approaches as desired. For example, you could use the parse method, then the addRecipient method.
Note: You must add your mail subsystem information to your server.properties file. An example follows:
#=======================================================
# Mail subsystem
#=======================================================
# Comma-delimited list of all smtp servers capable of
# relaying mail to the internet
mail.system.mail.smtp.host: mail
# What port is the MTA listening on
mail.system.mail.smtp.port: 25
# Number of milliseconds to socket connet timeout. When
# this limit is reached, we try the next mail server.
mail.system.mail.smtp.connectiontimeout: 2000
# Base mail template directory
mail.templateRoot: $webRoot/config/mailTemplates
# What email account to use when the system sends out
# mails
mail.systemFromAddress: website@companyName.com
mail.helpdeskAddress: support@companyName.com
mail.URLPrefix: http://www.companyName.com
-
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a header to the email.voidaddHeaders(Map headers) Adds a set of headers to the email.voidaddRecipient(String recipient) Adds a recipient to the email.voidaddRecipient(String recipient, jakarta.mail.Message.RecipientType type) Adds a recipient of the specified type to the email.voidaddRecipients(String recipients) Adds a comma-delimited list of recipients to the email.voidaddRecipients(String recipients, jakarta.mail.Message.RecipientType type) Adds a comma-delimited list of recipients of the specified type to the email.voidaddRecipients(List recipients) Adds a list of recipients to the email.voidaddRecipients(List recipients, jakarta.mail.Message.RecipientType type) Adds a list of recipients of the specified type to the email.voidaddReplyTo(String replyTo) Adds a "reply to" address to the email.Gets theWriterstream for the message body.voidreset()Resets MailMessage state.voidsend()Sends the email message.voidSets if SMTP connection should be authorized.voidAdds the message body to the email.voidsetContextMap(Map value) Sets the context map to use with this message.voidSets the "from" address of the email.voidSets SMTP host or Velocity expression.voidsetPassword(String password) Sets SMTP connection password or Velocity expression.voidSets SMTP port or Velocity expression.voidsetProperty(String name, String value) Additional property that will be passed toSessionclass.voidsetRecipient(String recipient) Adds a recipient to the email.voidsetReplyTo(String replyTo) Sets the "reply to" address for the email.voidsetReplyTo(List replyTo) Sets the parameter list of addresses passed in as the "reply to" addresses for the email.voidsetSubject(String subject) Sets the subject of the email.voidSets SMTP connection user or Velocity expression.
-
Method Details
-
setContextMap
Sets the context map to use with this message. Velocity looks in the context map to find its substitution values- Parameters:
value- The new context map
-
setFrom
Sets the "from" address of the email.- Parameters:
sender- the "from" address
-
addReplyTo
Adds a "reply to" address to the email.- Parameters:
replyTo- the "reply to" address
-
setReplyTo
Sets the "reply to" address for the email. If the provided String contains commas, we assume that it is a comma-separated list of addresses; otherwise, it is treated as a String containing a single address- Parameters:
replyTo- the "reply to" address
-
setReplyTo
Sets the parameter list of addresses passed in as the "reply to" addresses for the email.- Parameters:
replyTo- the "reply to" address
-
setSubject
Sets the subject of the email.- Parameters:
subject- the subject
-
setHost
Sets SMTP host or Velocity expression.- Parameters:
host- host
-
setPort
Sets SMTP port or Velocity expression.- Parameters:
port- integer value asStringor Velocity expression
-
setAuth
Sets if SMTP connection should be authorized.- Parameters:
auth- boolean value asStringor Velocity expression
-
setUser
Sets SMTP connection user or Velocity expression.- Parameters:
user- user
-
setPassword
Sets SMTP connection password or Velocity expression.- Parameters:
password- password
-
addHeader
Adds a header to the email.- Parameters:
field- the name of the headervalue- the header value
-
addHeaders
Adds a set of headers to the email.- Parameters:
headers- the set of headers to be added
-
addRecipient
Adds a recipient to the email.- Parameters:
recipient- the email address of the recipient
-
setRecipient
Adds a recipient to the email. This method here for DataTools.setProperties() style interaction.- Parameters:
recipient- the email address of the recipient
-
addRecipients
Adds a list of recipients to the email.- Parameters:
recipients- the list of email addresses of the recipients
-
addRecipients
Adds a comma-delimited list of recipients to the email.- Parameters:
recipients- the comma-delimited list of email addresses of the recipients
-
addRecipient
Adds a recipient of the specified type to the email.- Parameters:
recipient- the email address of the recipienttype- the type of recipient, for example,Message.RecipientType.TO
-
addRecipients
Adds a list of recipients of the specified type to the email.- Parameters:
recipients- the list of email addresses of the recipientstype- the type of the recipientsfor example,Message.RecipientType.TO
-
addRecipients
Adds a comma-delimited list of recipients of the specified type to the email.- Parameters:
recipients- the comma-delimited list of email addresses of the recipientstype- the types of the recipients for example,Message.RecipientType.TO
-
setBody
Adds the message body to the email.- Parameters:
newBody- the message body
-
setProperty
Additional property that will be passed toSessionclass. All properties set viasetPropertywill override properties set by dedicated setters likesetHost(...)and any related properties configured inserver.properties.Note, that property values also support Velocity templates.
- Parameters:
name- property name, omitting"mail.smtp."prefixvalue- property value
-
getBodyWriter
Gets theWriterstream for the message body.- Returns:
- the
Writerstream for the message body
-
send
Sends the email message.- Throws:
IOException- if an I/O error occurs while trying to send the emailException
-
reset
public void reset()Resets MailMessage state.
-