Package de.aristaflow.adept2.model.mail
Class Email
- java.lang.Object
-
- de.aristaflow.adept2.model.mail.Email
-
- All Implemented Interfaces:
Closeable
,Serializable
,AutoCloseable
public class Email extends Object implements Serializable, Closeable
A simple data structure and builder for an email. You have to set at least one recipient, the subject and some text content of the mail. When not using aMailService
you will have to set the sender of the mail.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
ATTACHMENT_NAME
The name for an attachment if not set explicitly.protected Cleanup<IOException>
cleanup
The clean-up for closing theattachments
as post-mortem task for this instance.protected Logger
logger
The logger for problems with attachments (rather the corresponding streams).
-
Constructor Summary
Constructors Constructor Description Email()
Deprecated, for removal: This API element is subject to removal in a future version.UseEmail(Logger)
instead.Email(Logger logger)
Creates a new email to be built using the corresponding builder methods.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Email
attachment(SerialisableAttachment att)
Adds the designated attachment to this mail.Email
attachment(String fileName, UDTValue attData, boolean embed)
Adds the content of the designated UDT value as attachment to this mail.Email
attachment(String fileName, String contentType, byte[] attData, boolean embed)
Adds the designated content as attachment to this mail.Email
attachment(String fileName, String contentType, UDTValue attData, boolean embed)
Adds the content of the designated UDT value as attachment to this mail.Email
attachment(String fileName, String contentType, URI attSource, boolean embed)
Adds the content of the designated URI as attachment to this mail.Email
attachment(String fileName, String contentType, URL attSource, boolean embed)
Adds the content of the designated URL as attachment to this mail.Email
attachment(String fileName, String contentType, javax.activation.DataSource attSource, boolean embed)
Adds the designated attachment to this mail.Email
attachment(URI attSource, boolean embed)
Adds the content of the designated URI as attachment to this mail.Email
attachment(URL attSource, boolean embed)
Adds the content of the designated URL as attachment to this mail.Email
attachment(javax.activation.DataSource attSource, boolean embed)
Adds the designated attachment to this mail.Email
bcc(String address)
Sets an address to which to send a copy of the mail unaware of the other recipients.Email
bcc(String name, String address)
Sets an address to which to send a copy of the mail unaware of the other recipients.Email
bounceTo(String address)
Sets the address to which to send problems that occur on the way of the mail to its recipient.Email
bounceTo(String name, String address)
Sets the address to which to send problems that occur on the way of the mail to its recipient.Email
cc(String address)
Sets an address to which to send a copy of the mail.Email
cc(String name, String address)
Sets an address to which to send a copy of the mail.Email
clearAttachments()
Removes all attachments from this mail.Email
clearBcc()
Removes all addresses to which to send copies of the mail unaware of the other recipients.Email
clearBounceTo()
Removes the address to which to send problems that occur on the way of the mail to its recipient.Email
clearCc()
Removes all addresses to which to send copies of the mail.Email
clearFrom()
Removes the address of the sender.Email
clearHeaders()
Removes all additional headers from this mail.Email
clearPriority()
Removes the priority.Email
clearReplyTo()
Removes the address to which to reply to.Email
clearTo()
Removes all addresses to which to send the mail.void
close()
Email
from(String address)
Sets the address of the sender.Email
from(String name, String address)
Sets the address and the name of the sender.List<SerialisableAttachment>
getAttachments()
Gets the files attached to the mail or an empty list.List<EmailAddress>
getBcc()
Gets the addresses to which to send a copy of the mail unaware of the other recipients or an empty list.EmailAddress
getBounceTo()
Gets the address to which to send problems that occur on the way of the mail to its recipient ornull
.List<EmailAddress>
getCc()
Gets the addresses to which to send a copy of the mail or an empty list.EmailAddress
getFrom()
Gets the address of the sender.Map<String,String>
getHeaders()
Gets additional headers of the mail or an empty mapEmailPriority
getPriority()
Gets the priority of this mail ornull
.EmailAddress
getReplyTo()
Gets the address to which to reply to (in case this differs from the sender) ornull
.String
getSubject()
Gets the subject of the mail.String
getText()
Gets the text content of the mail, i. e. the text mail body.List<EmailAddress>
getTo()
Gets the addresses to which to send the mail.Email
header(String name, String value)
Adds the designated key-value-pair as additional header of this mail.Email
html(String htm)
Sets the content of the mail as HTML formatted text.boolean
isHtml()
Gets whether the text content of the mail is HTML (instead of plain text) and should be formatted appropriately.Email
priority(int intPrio)
Sets the priority.Email
priority(EmailPriority prio)
Sets the priority.Email
priority(String strPrio)
Sets the priority.Email
replyTo(String address)
Sets the address to which to reply to (in case this differs from the sender).Email
replyTo(String name, String address)
The address to which to reply to (in case this differs from the sender).Email
subject(String subj)
Sets the subject of the mail.Email
text(String txt)
Sets the text content of the mail, i. e. the text mail body.Email
to(String address)
Sets an address to which to send the mail.Email
to(String name, String address)
Sets an address to which to send the mail.
-
-
-
Field Detail
-
ATTACHMENT_NAME
protected static final String ATTACHMENT_NAME
The name for an attachment if not set explicitly. Rather useless.- See Also:
- Constant Field Values
-
logger
protected transient Logger logger
The logger for problems with attachments (rather the corresponding streams).
-
cleanup
protected transient Cleanup<IOException> cleanup
The clean-up for closing theattachments
as post-mortem task for this instance.
-
-
Constructor Detail
-
Email
@Deprecated(since="15.0.0", forRemoval=true) public Email()
Deprecated, for removal: This API element is subject to removal in a future version.UseEmail(Logger)
instead.Creates a new email to be built using the corresponding builder methods. Initially the email is empty and cannot be used.
-
Email
public Email(Logger logger)
Creates a new email to be built using the corresponding builder methods. Initially the email is empty and cannot be used.- Parameters:
logger
- The logger for problems with attachments (rather the corresponding streams).
-
-
Method Detail
-
getFrom
public EmailAddress getFrom()
Gets the address of the sender. This may benull
when using aMailService
for sending this mail. In this case theMailService
will set a default sending address.- Returns:
- The address of the sender or
null
.
-
getReplyTo
public EmailAddress getReplyTo()
Gets the address to which to reply to (in case this differs from the sender) ornull
.- Returns:
- The address to which to reply to (in case this differs from the sender) or
null
.
-
getBounceTo
public EmailAddress getBounceTo()
Gets the address to which to send problems that occur on the way of the mail to its recipient ornull
.- Returns:
- The address to which to send problems that occur on the way of the mail to its
recipient or
null
.
-
getTo
public List<EmailAddress> getTo()
Gets the addresses to which to send the mail. This has to contain at least one valid address.- Returns:
- The addresses to which to send the mail.
-
getCc
public List<EmailAddress> getCc()
Gets the addresses to which to send a copy of the mail or an empty list.- Returns:
- The addresses to which to send a copy of the mail or an empty list.
-
getBcc
public List<EmailAddress> getBcc()
Gets the addresses to which to send a copy of the mail unaware of the other recipients or an empty list.- Returns:
- The addresses to which to send a copy of the mail unaware of the other recipients.
-
getSubject
public String getSubject()
Gets the subject of the mail. This has to be set before sending the mail.- Returns:
- The subject of the mail.
-
getText
public String getText()
Gets the text content of the mail, i. e. the text mail body. This has to be set before sending the mail.- Returns:
- The text content of the mail, i. e. the text mail body. This has to be set before sending the mail.
-
isHtml
public boolean isHtml()
Gets whether the text content of the mail is HTML (instead of plain text) and should be formatted appropriately.- Returns:
- Whether the text content of the mail is HTML (instead of plain text) and should be formatted appropriately.
-
getPriority
public EmailPriority getPriority()
Gets the priority of this mail ornull
.- Returns:
- The priority of this mail or
null
.
-
getAttachments
public List<SerialisableAttachment> getAttachments()
Gets the files attached to the mail or an empty list.- Returns:
- The files attached to the mail or an empty list.
-
getHeaders
public Map<String,String> getHeaders()
Gets additional headers of the mail or an empty map./- Returns:
- Additional headers of the mail or an empty map.
-
from
public Email from(String address)
Sets the address of the sender.- Parameters:
address
- The address of the sender.- Returns:
- This
Email
.
-
from
public Email from(String name, String address)
Sets the address and the name of the sender.- Parameters:
name
- The name of the sender.address
- The address of the sender.- Returns:
- This
Email
.
-
clearFrom
public Email clearFrom()
Removes the address of the sender.- Returns:
- This
Email
.
-
replyTo
public Email replyTo(String address)
Sets the address to which to reply to (in case this differs from the sender).- Parameters:
address
- The address to which to reply to.- Returns:
- This
Email
.
-
replyTo
public Email replyTo(String name, String address)
The address to which to reply to (in case this differs from the sender).- Parameters:
name
- The name to which to reply to.address
- The address to which to reply to.- Returns:
- This
Email
.
-
clearReplyTo
public Email clearReplyTo()
Removes the address to which to reply to.- Returns:
- This
Email
.
-
bounceTo
public Email bounceTo(String address)
Sets the address to which to send problems that occur on the way of the mail to its recipient.- Parameters:
address
- The address to which to send problems that occur on the way of the mail to its recipient.- Returns:
- This
Email
.
-
bounceTo
public Email bounceTo(String name, String address)
Sets the address to which to send problems that occur on the way of the mail to its recipient.- Parameters:
name
- The name to which to send problems that occur on the way of the mail to its recipient.address
- The address to which to send problems that occur on the way of the mail to its recipient.- Returns:
- This
Email
.
-
clearBounceTo
public Email clearBounceTo()
Removes the address to which to send problems that occur on the way of the mail to its recipient.- Returns:
- This
Email
.
-
to
public Email to(String address)
Sets an address to which to send the mail. At least one address has to be set before sending the mail.- Parameters:
address
- An address to which to send the mail.- Returns:
- This
Email
.
-
to
public Email to(String name, String address)
Sets an address to which to send the mail. At least one address has to be set before sending the mail.- Parameters:
name
- A name to which to send the mail.address
- An address to which to send the mail.- Returns:
- This
Email
.
-
clearTo
public Email clearTo()
Removes all addresses to which to send the mail.- Returns:
- This
Email
.
-
cc
public Email cc(String address)
Sets an address to which to send a copy of the mail.- Parameters:
address
- An address to which to send a copy of the mail.- Returns:
- This
Email
.
-
cc
public Email cc(String name, String address)
Sets an address to which to send a copy of the mail.- Parameters:
name
- A name to which to send a copy of the mail.address
- An address to which to send a copy of the mail.- Returns:
- This
Email
.
-
clearCc
public Email clearCc()
Removes all addresses to which to send copies of the mail.- Returns:
- This
Email
.
-
bcc
public Email bcc(String address)
Sets an address to which to send a copy of the mail unaware of the other recipients.- Parameters:
address
- An address to which to send a copy of the mail unaware of the other recipients.- Returns:
- This
Email
.
-
bcc
public Email bcc(String name, String address)
Sets an address to which to send a copy of the mail unaware of the other recipients.- Parameters:
name
- A name to which to send a copy of the mail unaware of the other recipients.address
- An address to which to send a copy of the mail unaware of the other recipients.- Returns:
- This
Email
.
-
clearBcc
public Email clearBcc()
Removes all addresses to which to send copies of the mail unaware of the other recipients.- Returns:
- This
Email
.
-
subject
public Email subject(String subj)
Sets the subject of the mail. This has to be set before sending the mail.- Parameters:
subj
- The subject of the mail.- Returns:
- This
Email
.
-
text
public Email text(String txt)
Sets the text content of the mail, i. e. the text mail body. This has to be set before sending the mail.- Parameters:
txt
- The text content of the mail, i. e. the text mail body. This has to be set before sending the mail.- Returns:
- This
Email
.
-
html
public Email html(String htm)
Sets the content of the mail as HTML formatted text. This replacestext(String)
. This has to be set before sending the mail.- Parameters:
htm
- The HTML formatted text content of the mail, i. e. the text mail body. This has to be set before sending the mail.- Returns:
- This
Email
.
-
priority
public Email priority(int intPrio)
Sets the priority.- Parameters:
intPrio
- The integer value of the priority to set.- Returns:
- This
Email
.
-
priority
public Email priority(String strPrio)
Sets the priority.- Parameters:
strPrio
- The string value of the priority to set (the same name as the constants with space instead of underscore and case-insensitive).- Returns:
- This
Email
.
-
priority
public Email priority(EmailPriority prio)
Sets the priority.- Parameters:
prio
- The priority to set.- Returns:
- This
Email
.
-
clearPriority
public Email clearPriority()
Removes the priority.- Returns:
- This
Email
.
-
attachment
public Email attachment(SerialisableAttachment att)
Adds the designated attachment to this mail.- Parameters:
att
- The attachment.- Returns:
- This
Email
.
-
attachment
public Email attachment(String fileName, UDTValue attData, boolean embed)
Adds the content of the designated UDT value as attachment to this mail. If the UDT name of the designated contains a/
, the name will be assumed to be a valid content type and thus used appropriately. Otherwiseapplication/octet-stream
will be used as content type.- Parameters:
fileName
- The file name of the attachment. This must not benull
nor blank.attData
- The actual content. The caller is responsible for closing.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
.
-
attachment
public Email attachment(String fileName, String contentType, UDTValue attData, boolean embed)
Adds the content of the designated UDT value as attachment to this mail.- Parameters:
fileName
- The file name of the attachment. This must not benull
nor blank.contentType
- The content type of the file. If this isnull
,application/octet-stream
will be used.attData
- The actual content. The caller is responsible for closing.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
.
-
attachment
public Email attachment(String fileName, String contentType, byte[] attData, boolean embed)
Adds the designated content as attachment to this mail.- Parameters:
fileName
- The file name of the attachment. This must not benull
nor blank.contentType
- The content type of the file. If this isnull
,application/octet-stream
will be used.attData
- The actual content.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
.
-
attachment
public Email attachment(URL attSource, boolean embed)
Adds the content of the designated URL as attachment to this mail.- Parameters:
attSource
- The URL used as data source, providing name, the content type and the actual attachment content.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
.
-
attachment
public Email attachment(String fileName, String contentType, URL attSource, boolean embed)
Adds the content of the designated URL as attachment to this mail.- Parameters:
fileName
- The file name of the attachment. If this isnull
or blank, default name will be used.contentType
- The content type of the attachment. If this isnull
,application/octet-stream
will be used.attSource
- The URL from which to retrieve the content.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
.
-
attachment
public Email attachment(URI attSource, boolean embed) throws MalformedURLException
Adds the content of the designated URI as attachment to this mail.- Parameters:
attSource
- The URI used as data source, providing name, the content type and the actual attachment content.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
. - Throws:
MalformedURLException
- If there are problems converting the designated URI to a URL, aMalFormedURLException
will be thrown.
-
attachment
public Email attachment(String fileName, String contentType, URI attSource, boolean embed) throws MalformedURLException
Adds the content of the designated URI as attachment to this mail.- Parameters:
fileName
- The file name of the attachment. If this isnull
or blank, default name will be used.contentType
- The content type of the attachment. If this isnull
,application/octet-stream
will be used.attSource
- The URI from which to retrieve the content.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
. - Throws:
MalformedURLException
- If there are problems converting the designated URI to a URL, aMalFormedURLException
will be thrown.
-
attachment
public Email attachment(javax.activation.DataSource attSource, boolean embed)
Adds the designated attachment to this mail.- Parameters:
attSource
- The data source providing name, the content type and the actual attachment content.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
.
-
attachment
public Email attachment(String fileName, String contentType, javax.activation.DataSource attSource, boolean embed)
Adds the designated attachment to this mail.- Parameters:
fileName
- The file name of the attachment. If this isnull
or blank the name of the designated data source will be used. If this is alsonull
or blank, default name will be used.contentType
- The content type of the attachment. If this is notnull
, it will override the content type of the designated data source.attSource
- The data source providing the actual attachment content.embed
- Whether to embed this attachment, i. e. hiding it from the list of attachments.- Returns:
- This
Email
.
-
clearAttachments
public Email clearAttachments()
Removes all attachments from this mail.- Returns:
- This
Email
.
-
header
public Email header(String name, String value)
Adds the designated key-value-pair as additional header of this mail.- Parameters:
name
- The name of the additional header.value
- The value of the additional header. This may benull
.- Returns:
- This
Email
.
-
clearHeaders
public Email clearHeaders()
Removes all additional headers from this mail.- Returns:
- This
Email
.
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
-