Class Mail
This class is not thread safe.
- Author:
- Christopher Mindus
Method Summary
Modifier and TypeMethodDescriptionbooleanaddAttachment(File file) Adds a file to the contents.voidaddBCC(InternetAddress... bccs) Adds the BCC address(es).voidAdds the BCC address(es).voidaddCC(InternetAddress... ccs) Adds the CC address(es).voidAdds the CC address(es).voidaddDestination(InternetAddress... dests) Adds the destination address(es).voidaddDestination(String dest) Adds the destination address(es).protected voidfinalize()Finalize, just in case to delete attachments.Gets the javax.mail.Message for advanced purposes.booleanqueue()Queues the mail for sending when time comes.booleanqueue(boolean log) Queues the mail for sending when time comes.booleansend()Sends the mail directly without passing through the queue.booleansend(boolean log) Sends the mail directly without passing through the queue.voidsetFrom(InternetAddress from) Sets the From address, normally configured in the Server.voidSets the From address, normally configured in the Server.voidsetSubject(String subject) Sets the subject.voidsetText(PlainKString text, IKStringInfoProvider provider) Sets a KString text with either HTML or plain text contents.voidSets a simple text encoded as UTF-8.voidsetText(String htmlPrefix, KString text, String htmlSuffix, IKStringInfoProvider provider) Sets a KString text with either HTML or plain text contents.voidSets the text from a HTML document with a set of parameters that will replace the contents of the HTML document text.
Method Details
finalize
Finalize, just in case to delete attachments.getMessage
Gets the javax.mail.Message for advanced purposes.- Returns:
- The message, or null if it already has been send or queued.
setSubject
public void setSubject(String subject) throws IllegalStateException, IllegalWriteException, MessagingException Sets the subject.- Parameters:
subject- The subject of the mail, null for no subject (not recommended).- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.MessagingException- For other failures.
addDestination
public void addDestination(String dest) throws IllegalStateException, AddressException, MessagingException Adds the destination address(es). Adresses are separated by commas.The address(es) are parsed with given sequence of addresses into
InternetAddressobjects. The RFC822 syntax rules are enforced.- Parameters:
dest- The destination(s) to add.- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.AddressException- If the address is invalid.MessagingException- For other failures.
addDestination
public void addDestination(InternetAddress... dests) throws IllegalStateException, MessagingException Adds the destination address(es).- Parameters:
dests- A list of destination address(es).- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.MessagingException- For other failures.
addCC
Adds the CC address(es). Adresses are separated by commas.The address(es) are parsed with given sequence of addresses into
InternetAddressobjects. The RFC822 syntax rules are enforced.- Parameters:
ccs- The CC address(es) to add.- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.AddressException- If the address is invalid.MessagingException- For other failures.
addCC
Adds the CC address(es).- Parameters:
ccs- List of CC addresses.- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.MessagingException- For other failures.
addBCC
Adds the BCC address(es). Adresses are separated by commas.The address(es) are parsed with given sequence of addresses into
InternetAddressobjects. The RFC822 syntax rules are enforced.- Parameters:
bccs- The BCC destination address(es) to add.- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.AddressException- If the address is invalid.MessagingException- For other failures.
addBCC
Adds the BCC address(es).- Parameters:
bccs- The list of BCC destination address(es) to add.- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.MessagingException- For other failures.
setFrom
Sets the From address, normally configured in the Server. This will parse the given string and create anInternetAddress. The address is parsed using "strict" parsing. No additional syntax checks that are performed here.This call is equivalent to
InternetAddress(address, false).- Parameters:
from- The address in RFC822 format.- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.AddressException- If the address is invalid.MessagingException- For other failures.
setFrom
Sets the From address, normally configured in the Server.- Parameters:
from- The From address.- Throws:
IllegalWriteException- If the underlying implementation does not support modification of existing values.IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.MessagingException- For other failures.
setText
Sets a simple text encoded as UTF-8.This convenience method that sets the given String as this part's content with a MIME type of "text/plain".
Any previous content is replaced.
- Parameters:
text- The text.- Throws:
IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.
setText
Sets a KString text with either HTML or plain text contents. Any references to images in the HTML KString are added into the mail as pure references, i.e. they will be loaded from the (this) sender server.This convenience method that sets the given String as this part's content with a MIME type of "text/plain".
Any previous content is replaced.
- Parameters:
text- The text in HTML or plain text.provider- The KString information provider.- Throws:
IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.
setText
public void setText(String htmlPrefix, KString text, String htmlSuffix, IKStringInfoProvider provider) throws IllegalStateException Sets a KString text with either HTML or plain text contents. Any references to images in the HTML KString are added into the mail as pure references, i.e. they will be loaded from the (this) sender server.This convenience method that sets the given String as this part's content with a MIME type of "text/plain".
The
htmlPrefixandhtmlSuffixare important to specify because they are not allowed in a KString.Any previous content is replaced.
- Parameters:
htmlPrefix- The prefix string in HTML, including the starting<body>tag, null for default<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body>text- The text in HTML or plain text.htmlSuffix- The suffix string in HTML, including the ending</body>tag, null for default</body> </html>provider- The KString information provider.- Throws:
IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.
setText
Sets the text from a HTML document with a set of parameters that will replace the contents of the HTML document text. If a parameter isnull, all HTML text within%%KEY(%%and%%)KEY%%will be removed. If the parameter isnon-null, all HTML texts as%%KEY(%%and%%)KEY%%will be deleted and each occurrence of the%%KEY%%will have theValueof theKeyreplaced with the HTML escaped value.- Parameters:
text- The HTML document text.map- The map of keys and values, where a value can benullto remove HTML text from the document.doLineBreaks- Flag to replace CR/LF combinations with '<br>' when true, false not to replace and leave as is.- Throws:
IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.IllegalArgumentException- If there is a mismatch in occurrences of%%KEY(%%and%%)KEY%%.- See Also:
addAttachment
Adds a file to the contents. This will transform the message to a multipart message and requires a previous call tosetText(String)orsetText(PlainKString, IKStringInfoProvider).- Parameters:
file- The file attachment, do not overdo it, in general, 20 MB of attachments is accepted, but it depends on the providers.- Returns:
- true if attachment is added, false if already attached.
- Throws:
IllegalStateException- If this message is obtained from a READ_ONLY folder, or if the message is already sent.IOException- If the attachment is not a file or can't be read.
send
Sends the mail directly without passing through the queue. The current thread will wait until mail is sent.Any errors are logged, i.e. same as calling
send(boolean)withtrueas parameter.Any left-overs (directories, attached file copies) are disposed of directly.
- Returns:
- true if OK, false if the mail is already sent or queued for sending.
- Throws:
MessagingException- For mail sending errors.
send
Sends the mail directly without passing through the queue. The current thread will wait until mail is sent.Any left-overs (directories, attached file copies) are disposed of directly.
- Parameters:
log- Flag to log the error, if any.- Returns:
- true if OK, false if the mail is already sent or queued for sending.
- Throws:
MessagingException- For mail sending errors.
queue
Queues the mail for sending when time comes. The current thread does not have to wait for the sending process.Any errors are logged, i.e. same as calling
queue(boolean).trueas parameter.- Returns:
- true if OK, false if the mail is already sent or queued for sending. false is also returned if the mail sender is disposed of.
- Throws:
MessagingException- For mail sending errors.IOException- If the mail failed to be saved.
queue
Queues the mail for sending when time comes. The current thread does not have to wait for the sending process.- Parameters:
log- Flag to log the failure.- Returns:
- true if OK, false if the mail is already sent or queued for sending. false is also returned if the mail sender is disposed of.
- Throws:
MessagingException- For mail sending errors.IOException- If the mail failed to be saved.