public class MailSender extends Object
Modifier and Type | Field and Description |
---|---|
protected List<DataSource> |
attachments |
protected String |
bounceToken |
protected static String |
CONFIG_KEY_HEADERS |
protected Map<String,String> |
headers |
protected String |
html |
protected sirius.kernel.commons.Context |
htmlContext |
protected String |
htmlTemplate |
protected String |
lang |
protected String |
receiverEmail |
protected String |
receiverName |
protected String |
senderEmail |
protected String |
senderName |
protected boolean |
simulate |
protected String |
subject |
protected String |
text |
protected sirius.kernel.commons.Context |
textContext |
protected String |
textTemplate |
protected String |
type |
Modifier | Constructor and Description |
---|---|
protected |
MailSender() |
Modifier and Type | Method and Description |
---|---|
MailSender |
addAttachment(DataSource attachment)
Adds an attachment to the email.
|
MailSender |
addAttachments(DataSource... attachmentsToAdd)
Adds an array of attachments to the email.
|
MailSender |
addAttachments(List<DataSource> attachments)
Adds a list of attachments to the email.
|
MailSender |
addHeader(String name,
String value)
Adds an individual header to the SMTP message.
|
String |
addResourceAsAttachment(String resource,
String filename)
Adds a resource as attachment.
|
MailSender |
addResourceAsAttachment(String resource,
String filename,
String contentId)
Adds a resource as attachment.
|
MailSender |
from(String senderEmail,
String senderName)
Specifies both, the sender email and name.
|
MailSender |
fromEmail(String senderEmail)
Sets the email address used as sender of the email.
|
MailSender |
fromName(String senderName)
Sets the name used as sender of the mail.
|
MailSender |
htmlContent(String html)
Sets the HTML content of the email.
|
MailSender |
htmlTemplate(String template,
sirius.kernel.commons.Context context)
Renders the given template and uses it as HTML part.
|
void |
send()
Sends the mail using the given settings.
|
protected void |
sendMailAsync(SMTPConfiguration config) |
MailSender |
setBounceToken(String token)
Sets a bounce token.
|
MailSender |
setLang(String... langs)
Sets the language used to perform
NLS lookups when rendering templates. |
MailSender |
simulate(boolean simulateOnly)
Sets the simulation flag.
|
MailSender |
subject(String subject)
Specifies the subject line of the mail.
|
MailSender |
textContent(String text)
Sets the text content of the email.
|
MailSender |
textTemplate(String template,
sirius.kernel.commons.Context context)
Renders the given template and uses it as text part.
|
MailSender |
to(String receiverEmail,
String receiverName)
Specifies both, the receiver email and name.
|
MailSender |
toEmail(String receiverEmail)
Specifies the email address to send the mail to.
|
MailSender |
toName(String receiverName)
Specifies the name of the receiver.
|
MailSender |
type(String type)
Specifies the type of the mail primarily used for logging.
|
protected static final String CONFIG_KEY_HEADERS
protected boolean simulate
protected String senderEmail
protected String senderName
protected String receiverEmail
protected String receiverName
protected String subject
protected sirius.kernel.commons.Context textContext
protected sirius.kernel.commons.Context htmlContext
protected String textTemplate
protected String htmlTemplate
protected String text
protected String html
protected String type
protected List<DataSource> attachments
protected String bounceToken
protected String lang
public MailSender fromEmail(String senderEmail)
senderEmail
- the address used as sender of the email.public MailSender fromName(String senderName)
senderName
- the senders name of the emailpublic MailSender toEmail(String receiverEmail)
receiverEmail
- the target address of the emailpublic MailSender toName(String receiverName)
receiverName
- the name of the receiverpublic MailSender from(String senderEmail, String senderName)
This is boilerpalte for fromEmail(senderEmail).fromName(senderName)
senderEmail
- the email address which sent the emailsenderName
- the name of the sender of the emailpublic MailSender to(String receiverEmail, String receiverName)
This is boilerpalte for toEmail(receiverEmail).toName(receiverName)
receiverEmail
- the email address which should receive the emailreceiverName
- the name of the receiver of the emailpublic MailSender subject(String subject)
subject
- the subject line to use.public MailSender type(String type)
type
- the type to use.public MailSender addHeader(String name, String value)
name
- the name of the header to addvalue
- the value of the header to addpublic MailSender textContent(String text)
text
- the text content of the emailpublic MailSender textTemplate(String template, @Nonnull sirius.kernel.commons.Context context)
template
- the name of the template to rendercontext
- the context passed to the rendererpublic MailSender htmlContent(String html)
html
- the HTML content of the emailpublic MailSender htmlTemplate(String template, @Nonnull sirius.kernel.commons.Context context)
template
- the name of the template to rendercontext
- the context passed to the rendererpublic MailSender addAttachment(DataSource attachment)
Use Generator.generateAttachment(String)
to directly generate an attachment from a
template.
attachment
- the attachment to add to the emailpublic MailSender addResourceAsAttachment(@Nonnull String resource, @Nullable String filename, @Nullable String contentId)
This only adds a static file as attachment. Use addAttachment(DataSource)
and Generator.generateAttachment(String)
to evaluate a template.
resource
- the resource to lookup using Resources.resolve(String)
filename
- the filename to use for the attachmentcontentId
- the content id to reference it within HTML content. < and > are automatically added.
Note that most mail clients like to see a valid message id (xxx@domain.tld) here.public String addResourceAsAttachment(@Nonnull String resource, @Nullable String filename)
This can be called from within a template to reference the template directly.
resource
- the resource to lookup using Resources.resolve(String)
filename
- the filename to use for the attachmentaddResourceAsAttachment(String, String, String)
public MailSender addAttachments(DataSource... attachmentsToAdd)
attachmentsToAdd
- the attachments to addpublic MailSender addAttachments(List<DataSource> attachments)
attachments
- the attachments to addpublic MailSender setBounceToken(String token)
This bounce toke is hopefully included in a bounce email (generated if a mail cannot be delivered). This permits better bounce handling.
token
- the token to identify the mail by a bounde handler.public MailSender simulate(boolean simulateOnly)
A mail which is simulated (simulateOnly is true) will occur in the mail logs etc. but won't actually be sent.
simulateOnly
- true if the mail should just be simulated but not actually be sent.public MailSender setLang(String... langs)
NLS
lookups when rendering templates.langs
- an array of languages. The first non empty value is used.public void send()
Once all settings are validated, the mail is send in a separate thread so this method will
return rather quickly. Note that a HandledException
is thrown in case
of invalid settings (bad mail address etc.).
protected void sendMailAsync(SMTPConfiguration config)
Copyright © 2018. All rights reserved.