com.upokecenter.mail.Message
com.upokecenter.mail.Message
public final class Message extends Object
Represents an email message, and contains methods and properties for accessing and modifying email message data. This class implements the Internet Message Format (RFC 5322) and Multipurpose Internet Mail Extensions (MIME; RFC 2045-2047, RFC 2049).
Thread safety: This class is mutable; its properties can be changed. None of its instance methods are designed to be thread safe. Therefore, access to objects from this class must be synchronized if multiple threads can access them at the same time.
The following lists known deviations from the mail specifications (Internet Message Format and MIME):
- If a message has two or more Content-Type header fields, it is treated as having a content type of "application/octet-stream", unless one or more of the header fields is syntactically invalid.
- Illegal UTF-8 byte sequences appearing in header field values are replaced with replacement characters. Moreover, UTF-8 is parsed everywhere in header field values, even in those parts of some structured header fields where this appears not to be allowed. (UTF-8 is a character encoding for the Unicode character set.)
- This implementation can parse a message even if that message is without a From header field, without a Date header field, or without both.
- The To and Cc header fields are allowed to contain only comments and whitespace, but these "empty" header fields will be omitted when generating.
- There is no line length limit imposed when parsing header fields, except header field names.
- There is no line length limit imposed when parsing quoted-printable or base64 encoded bodies.
- If the transfer encoding is absent and the content type is "message/rfc822", bytes with values greater than 127 are still allowed, despite the default value of "7bit" for "Content-Transfer-Encoding".
- In the following cases, if the transfer encoding is absent, declared as 7bit, or treated as 7bit, bytes greater than 127 are still allowed:
- (a) The preamble and epilogue of multipart messages, which will be ignored.
- (b) If the charset is declared to
be
utf-8
. - (c) If the content type is "text/html" and the
charset is declared to be
us-ascii
, "windows-1252", "windows-1251", or "iso-8859-*" (all single byte encodings). - (d) In text/plain message bodies. Any bytes greater than 127 are replaced with the substitute character byte (0x1a).
- (e) In MIME message bodies (this is not a deviation from MIME, though). Any bytes greater than 127 are replaced with the substitute character byte (0x1a).
- If the message starts with the word "From" (and no other case variations of that word) followed by one or more space (U+0020) not followed by colon, that text and the rest of the text is skipped up to and including a line feed (U+000A). (See also RFC 4155, which describes the so-called "mbox" convention with "From" lines of this kind.)
- The name
ascii
is treated as a synonym forus-ascii
, despite being a reserved name under RFC 2046. The namescp1252
andutf8
are treated as synonyms forwindows-1252
andutf-8
, respectively, even though they are not IANA registered aliases. - The following deviations involve encoded words under RFC 2047:
- (a) If a sequence of encoded words decodes to a string with a CTL character (U+007F, or a character less than U+0020 and not TAB) after being converted to Unicode, the encoded words are left un-decoded.
- (b) This implementation can decode encoded words regardless of the character length of the line in which they appear. This implementation can generate a header field line with one or more encoded words even if that line is more than 76 characters long. (This implementation follows the recommendation in RFC 5322 to limit header field lines to no more than 78 characters, where possible; see also RFC 6532.)
It would be appreciated if users of this library contact the author if they find other ways in which this implementation deviates from the mail specifications or other applicable specifications.
If a MIME message or body part has an unrecognized transfer encoding, its content type is treated as "application/octet-stream" rather than the declared content type (in accordance with MIME) and its transfer encoding is treated as 7bit (a clarification to MIME).
This class currently doesn't support the "padding" parameter for message bodies with the media type "application/octet-stream" or treated as that media type (see RFC 2046 sec. 4.5.1).
In this implementation, if the content-transfer-encoding "quoted-printable" or "base64" occurs in a message or body part with content type "multipart/*" or "message/*" (other than "message/global", "message/global-headers", "message/global-disposition-notification", or "message/global-delivery-status"), that encoding is treated as unrecognized for the purpose of treating that message or body part as having a content type of "application/octet-stream" rather than the declared content type. This is a clarification to RFCs 2045 and 2049. (This may result in "misdecoded" messages because in practice, most if not all messages of this kind don't use quoted-printable or base64 encodings for the whole body, but may do so in the body parts they contain.)
This implementation can decode an RFC 2047 encoded word that uses ISO-2022-JP or ISO-2022-JP-2 (encodings that use code switching) even if the encoded word's payload ends in a different mode from "ASCII mode". (Each encoded word still starts in "ASCII mode", though.) This, however, is not a deviation to RFC 2047 because the relevant rule only concerns bringing the output device back to "ASCII mode" after the decoded text is displayed (see last paragraph of sec. 6.2) -- since the decoded text is converted to Unicode rather than kept as ISO-2022-JP or ISO-2022-JP-2, this is not applicable since there is no such thing as "ASCII mode" in the Unicode Standard.
Note that this library (the MailLib library) has no facilities for sending and receiving email messages, since that's outside this library's scope.
Constructors
Methods
Message AddAttachment(MediaType mediaType)
Adds an attachment with an empty body and with the given media type to this message.Message AddAttachment(InputStream inputStream, MediaType mediaType)
Adds an attachment to this message in the form of data from the given readable stream, and with the given media type.Message AddAttachment(InputStream inputStream, MediaType mediaType, String filename)
Adds an attachment to this message in the form of data from the given readable stream, and with the given media type and file name.Message AddAttachment(InputStream inputStream, String filename)
Adds an attachment to this message in the form of data from the given readable stream, and with the given file name.Message AddHeader(String name, String value)
Adds a header field to the end of the message's header.Message AddHeader(Map.Entry<String,String> header)
Adds a header field to the end of the message's header.Message AddInline(MediaType mediaType)
Adds an inline body part with an empty body and with the given media type to this message.Message AddInline(InputStream inputStream, MediaType mediaType)
Adds an inline body part to this message in the form of data from the given readable stream, and with the given media type.Message AddInline(InputStream inputStream, MediaType mediaType, String filename)
Adds an inline body part to this message in the form of data from the given readable stream, and with the given media type and file name.Message AddInline(InputStream inputStream, String filename)
Adds an inline body part to this message in the form of data from the given readable stream, and with the given file name.Message ClearHeaders()
Deletes all header fields in this message.static String DecodeHeaderValue(String name, String value)
Decodes RFC 2047 encoded words from the given header field value and returns a string with those words decoded.static String ExtractHeader(byte[] bytes, String headerFieldName)
Extracts the value of a header field from a byte array representing an email message.static Message FromMailtoUri(String uri)
Creates a message object from a MailTo URI (uniform resource identifier).static Message FromMailtoUri(URI uri)
Creates a message object from a MailTo URI (uniform resource identifier) in the form of a URI object.static Message FromMailtoUrl(String url)
Deprecated. Renamed to FromMailtoUri.String Generate()
Generates this message's data in text form.byte[] GenerateBytes()
Generates this message's data as a byte array, using the same algorithm as the Generate method.List<NamedAddress> GetAddresses(String headerName)
Gets a list of addresses contained in the header fields with the given name in this message.List<Message> GetAttachments()
Gets a list of descendant body parts of this message that are considered attachments.final List<NamedAddress> getBccAddresses()
Deprecated. Use GetAddresses(\Bcc) instead.byte[] GetBody()
Gets the byte array for this message's body.Message GetBodyMessage()
Returns the mail message contained in this message's body.final String getBodyString()
Deprecated. Use GetBodyString() instead.String GetBodyString()
Gets the body of this message as a text string.final List<NamedAddress> getCCAddresses()
Deprecated. Use GetAddresses(\Cc) instead.final ContentDisposition getContentDisposition()
Gets this message's content disposition.final MediaType getContentType()
Gets this message's media type.int[] GetDate()
Gets the date and time extracted from this message's Date header field (the value of which is found as though GetHeader("date") were called).final String getFileName()
Gets a file name suggested by this message for saving the message's body to a file.String GetFormattedBodyString()
Gets a Hypertext Markup Language (HTML) rendering of this message's text body.final List<NamedAddress> getFromAddresses()
Deprecated. Use GetAddresses(\From) instead.Map.Entry<String,String> GetHeader(int index)
Gets the name and value of a header field by index.String GetHeader(String name)
Gets the first instance of the header field with the specified name, using a basic case-insensitive comparison.String[] GetHeaderArray(String name)
Gets an array with the values of all header fields with the specified name, using a basic case-insensitive comparison.final List<Map.Entry<String,String>> getHeaderFields()
Gets a snapshot of the header fields of this message, in the order in which they appear in the message.final List<Message> getParts()
Gets a list of all the parts of this message.final String getSubject()
Gets this message's subject.final List<NamedAddress> getToAddresses()
Deprecated. Use GetAddresses(\To) instead.static Message MakeMultilingualMessage(List<Message> messages, List<String> languages)
Generates a multilingual message (see RFC 8255) from a list of messages and a list of language strings.static Message NewBodyPart()
Creates a message object with no header fields.Message RemoveHeader(int index)
Removes a header field by index.Message RemoveHeader(String name)
Removes all instances of the given header field from this message.Message SelectLanguageMessage(List<String> languages)
Selects a body part for a multiple-language message( multipart/multilingual) according to the given language priority list.Message SelectLanguageMessage(List<String> languages, boolean preferOriginals)
Selects a body part for a multiple-language message( multipart/multilingual) according to the given language priority list and original-language preference.Message SetBody(byte[] bytes)
Sets the body of this message to the given byte array.final void setContentDisposition(ContentDisposition value)
final void setContentType(MediaType value)
Message SetCurrentDate()
Sets this message's Date header field to the current time as its value, with an unspecified time zone offset.Message SetDate(int[] dateTime)
Sets this message's Date header field to the given date and time.Message SetHeader(int index, String value)
Sets the value of a header field by index without changing its name.Message SetHeader(int index, String name, String value)
Sets the name and value of a header field by index.Message SetHeader(int index, Map.Entry<String,String> header)
Sets the name and value of a header field by index.Message SetHeader(String name, String value)
Sets the value of this message's header field.Message SetHtmlBody(String str)
Sets the body of this message to the specified string in Hypertext Markup Language (HTML) format.final void setSubject(String value)
Message SetTextAndHtml(String text, String html)
Sets the body of this message to a multipart body with plain text and Hypertext Markup Language (HTML) versions of the same message.Message SetTextAndMarkdown(String text, String markdown)
Sets the body of this message to a multipart body with plain text, Markdown, and Hypertext Markup Language (HTML) versions of the same message.Message SetTextBody(String str)
Sets the body of this message to the specified plain text string.String ToMailtoUri()
Generates a MailTo URI (uniform resource identifier) corresponding to this message.String ToMailtoUrl()
Deprecated. Renamed to ToMailtoUri.
Method Details
ExtractHeader
public static String ExtractHeader(byte[] bytes, String headerFieldName)
Extracts the value of a header field from a byte array representing an email message. The return value is intended for display purposes, not for further processing, and this method is intended to be used as an error handling tool for email messages that are slightly malformed. (Note that malformed email messages ought to be treated with greater suspicion than well-formed email messages.).
Parameters:
bytes
- A byte array representing an email message.headerFieldName
- The name of the header field to extract. This name will be compared with the names of header fields in the given message using a basic case-insensitive comparison. (Two strings are equal in such a comparison, if they match after converting the basic upper-case letters A to Z (U+0041 to U+005A) in both strings to basic lower-case letters.).
Returns:
- The value of the first instance of the header field with the given
name. Leading space and/or tab bytes (0x20 and/or 0x09) and CR/LF
(0x0d/0x0a) pairs will be removed from the header field value, and the value
is treated as encoded in UTF-8 (an 8-bit encoding form of the Unicode
Standard) where illegally encoded UTF-8 is replaced as appropriate with
replacement characters (U+FFFD). Returns null if
bytes
is null, ifheaderFieldName
is null, is more than 997 characters long, or has a character less than U+0021 or greater than U+007E in the Unicode Standard, if a header field with that name does not exist, or if a body (even an empty one) does not follow the header fields.
NewBodyPart
public static Message NewBodyPart()
Creates a message object with no header fields.
Returns:
- A message object with no header fields.
SetCurrentDate
public Message SetCurrentDate()
Sets this message's Date header field to the current time as its value, with an unspecified time zone offset.
This method can be used when the message is considered complete and ready to be generated, for example, using the "Generate()" method.
Returns:
- This object.
getBccAddresses
@Deprecated public final List<NamedAddress> getBccAddresses()
Gets a list of addresses found in the BCC header field or fields.
Returns:
- A list of addresses found in the BCC header field or fields.
getBodyString
@Deprecated public final String getBodyString()
Gets the body of this message as a text string. See the
GetBodyString()
method.
Returns:
- The body of this message as a text string.
Throws:
UnsupportedOperationException
- See theGetBodyString()
method.
GetAttachments
public List<Message> GetAttachments()
Gets a list of descendant body parts of this message that are considered attachments. An attachment is a body part or descendant body part that has a content disposition with a type other than inline. This message itself is not included in the list even if it's an attachment as just defined.
Returns:
- A list of descendant body parts of this message that are considered attachments.
GetBodyString
public String GetBodyString()
Gets the body of this message as a text string. If this message's media type is "multipart/alternative", returns the result of this method for the last supported body part. For any other "multipart" media type, returns the result of this method for the first body part for which this method returns a text string.
Returns:
- The body of this message as a text string.
Throws:
UnsupportedOperationException
- This message is a multipart message without a supported body part; or this message has a content disposition with a type other than "inline"; or this message's media type is a non-multipart type and does not specify the use of a "charset" parameter, has no character encoding declared or assumed for it (which is usually the case for non-text messages), or has an unsupported character encoding.
getCCAddresses
@Deprecated public final List<NamedAddress> getCCAddresses()
Gets a list of addresses found in the CC header field or fields.
Returns:
- A list of addresses found in the CC header field or fields.
GetFormattedBodyString
public String GetFormattedBodyString()
Gets a Hypertext Markup Language (HTML) rendering of this message's text
body. This method currently supports any message for which
GetBodyString()
outputs a text string and treats the following media types
specially: text/plain with format = flowed
, text/enriched,
text/markdown (original Markdown).
REMARK: The Markdown implementation currently supports all features of original Markdown, except that the implementation:
- does not strictly check the placement of "block-level HTML elements",
- does not prevent Markdown content from being interpreted as such merely because it's contained in a "block-level HTML element", and
- does not deliberately use HTML escapes to obfuscate email addresses wrapped in angle-brackets.
UnsupportedOperationException
- No supported body part was found; see
GetBodyString()
for more information.
### getContentDisposition
public final ContentDisposition getContentDisposition()
Gets this message's content disposition. The content disposition specifies
how a user agent should display or otherwise handle this message. Can be set
to null. If set to a disposition or to null, updates the Content-Disposition
header field as appropriate. (There is no default content disposition if
this value is null, and disposition types unrecognized by the application
should be treated as "attachment"; see RFC 2183 sec. 2.8.).
**Returns:**
* This message's content disposition, or null if none is specified.
### setContentDisposition
public final void setContentDisposition(ContentDisposition value)
### getContentType
public final MediaType getContentType()
Gets this message's media type. When getting, the media type may differ in
certain cases from the value of the Content-Type header field, if any, and
may have a value even if the Content-Type header field is absent from this
message. If set to a media type, updates the Content-Type header field as
appropriate. Cannot be set to null.
**Returns:**
* This message's media type.
**Throws:**
* NullPointerException
- This value is being set and "value" is null.
### setContentType
public final void setContentType(MediaType value)
### getFileName
public final String getFileName()
Gets a file name suggested by this message for saving the message's body to a file. For more information on the algorithm, see ContentDisposition.MakeFilename.
This method generates a file name
based on the filename
parameter of the Content-Disposition header
field, if it exists, or on the name
parameter of the Content-Type
header field, otherwise.
Returns:
- A suggested name for the file. Returns the empty string if there is no filename suggested by the content type or content disposition, or if that filename is an empty string.
GetAddresses
public List<NamedAddress> GetAddresses(String headerName)
Gets a list of addresses contained in the header fields with the given name in this message.
Parameters:
headerName
- The name of the header fields to retrieve.
Returns:
- A list of addresses, in the order in which they appear in this message's header fields of the given name.
Throws:
UnsupportedOperationException
- The parameterheaderName
is not supported for this method. Currently, the only header fields supported are To, Cc, Bcc, Reply-To, Sender, and From.NullPointerException
- The parameterheaderName
is null.IllegalArgumentException
- The parameterheaderName
is empty.
getFromAddresses
@Deprecated public final List<NamedAddress> getFromAddresses()
Gets a list of addresses found in the From header field or fields.
Returns:
- A list of addresses found in the From header field or fields.
getHeaderFields
public final List<Map.Entry<String,String>> getHeaderFields()
Gets a snapshot of the header fields of this message, in the order in which they appear in the message. For each item in the list, the key is the header field's name (where any basic upper-case letters, U+0041 to U+005A, are converted to basic lower-case letters) and the value is the header field's value.
Returns:
- A snapshot of the header fields of this message.
getParts
public final List<Message> getParts()
Gets a list of all the parts of this message. This list is editable. This will only be used if the message is a multipart message.
Returns:
- A list of all the parts of this message. This list is editable. This will only be used if the message is a multipart message.
getSubject
public final String getSubject()
Gets this message's subject. The subject's value is found as though GetHeader("subject") were called.
Returns:
- This message's subject, or null if there is none.
setSubject
public final void setSubject(String value)
getToAddresses
@Deprecated public final List<NamedAddress> getToAddresses()
Gets a list of addresses found in the To header field or fields.
Returns:
- A list of addresses found in the To header field or fields.
AddHeader
public Message AddHeader(Map.Entry<String,String> header)
Adds a header field to the end of the message's header.
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
header
- A key/value pair. The key is the name of the header field, such as "From" or "Content-ID". The value is the header field's value.
Returns:
- This instance.
Throws:
NullPointerException
- The key or value ofheader
is null.IllegalArgumentException
- The header field name is too long or contains an invalid character, or the header field's value is syntactically invalid.
AddHeader
public Message AddHeader(String name, String value)
Adds a header field to the end of the message's header.
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
name
- Name of a header field, such as "From" or "Content-ID" .value
- Value of the header field.
Returns:
- This instance.
Throws:
NullPointerException
- The parametername
orvalue
is null.IllegalArgumentException
- The header field name is too long or contains an invalid character, or the header field's value is syntactically invalid.
Generate
public String Generate()
Generates this message's data in text form.
The generated message will have only Basic Latin code points (U+0000 to U+007F), and the transfer encoding will always be 7bit, quoted-printable, or base64 (the declared transfer encoding for this message will be ignored).
The following
applies to the following header fields: From, To, Cc, Bcc, Reply-To, Sender,
Resent-To, Resent-From, Resent-Cc, Resent-Bcc, and Resent-Sender. If the
header field exists, but has an invalid syntax, has no addresses, or appears
more than once, this method will combine the addresses into one header field
if possible (in the case of all fields given other than From and Sender),
and otherwise generate a synthetic header field with the display-name set to
the contents of all of the header fields with the same name, and the address
set to me@[header-name]-address.invalid
as the address (a
.invalid
address is a reserved address that can never belong to anyone).
(An exception is that the Resent-* header fields may appear more than once.)
The generated message should always have a From header field.
If a Date and/or Message-ID header field doesn't exist, a field with that name will be generated (using the current local time for the Date field).
When encoding the message's body, if the message has a text content type ("text/*"), the line breaks are a CR byte (carriage return, 0x0d) followed by an LF byte (line feed, 0x0a), CR alone, or LF alone. If the message has any other content type, only CR followed by LF is considered a line break.
Returns:
- The generated message.
Throws:
MessageDataException
- The message can't be generated.
GenerateBytes
public byte[] GenerateBytes()
Generates this message's data as a byte array, using the same algorithm as the Generate method.
Returns:
- The generated message as a byte array.
GetBody
public byte[] GetBody()
Gets the byte array for this message's body. This method doesn' t make a copy of that byte array.
Returns:
- A byte array.
GetDate
public int[] GetDate()
Gets the date and time extracted from this message's Date header field (the value of which is found as though GetHeader("date") were called). See MailDateTime.ParseDateString(string, boolean) for more information on the format of the date-time array returned by this method.
Returns:
- An array of 32-bit unsigned integers.
SetDate
public Message SetDate(int[] dateTime)
Sets this message's Date header field to the given date and time.
Parameters:
dateTime
-An array containing at least eight elements expressing a date and time. See MailDateTime.ParseDateString(string, boolean) for more information on this parameter.
Returns:
- This object.
Throws:
IllegalArgumentException
- The parameterdateTime
contains fewer than eight elements or contains invalid values (see MailDateTime.ParseDateString(string, boolean)).NullPointerException
- The parameterdateTime
is null.
GetBodyMessage
public Message GetBodyMessage()
Returns the mail message contained in this message's body.
Returns:
- A message object if this object's content type is "message/rfc822", "message/news", or "message/global", or null otherwise.
GetHeader
public Map.Entry<String,String> GetHeader(int index)
Gets the name and value of a header field by index.
Parameters:
index
- Zero-based index of the header field to get.
Returns:
- A key/value pair. The key is the name of the header field, such as "From" or "Content-ID". The value is the header field's value.
Throws:
IllegalArgumentException
- The parameterindex
is 0 or at least as high as the number of header fields.
GetHeader
public String GetHeader(String name)
Gets the first instance of the header field with the specified name, using a basic case-insensitive comparison. (Two strings are equal in such a comparison, if they match after converting the basic upper-case letters A to Z (U+0041 to U+005A) in both strings to basic lower-case letters.).
Parameters:
name
- The name of a header field.
Returns:
- The value of the first header field with that name, or null if there is none.
Throws:
NullPointerException
- Name is null.
GetHeaderArray
public String[] GetHeaderArray(String name)
Gets an array with the values of all header fields with the specified name, using a basic case-insensitive comparison. (Two strings are equal in such a comparison, if they match after converting the basic upper-case letters A to Z (U+0041 to U+005A) in both strings to basic lower-case letters.).
Parameters:
name
- The name of a header field.
Returns:
- An array containing the values of all header fields with the given name, in the order they appear in the message. The array will be empty if no header field has that name.
Throws:
NullPointerException
- Name is null.
ClearHeaders
public Message ClearHeaders()
Deletes all header fields in this message. Also clears this message's content disposition and resets its content type to MediaType.TextPlainAscii.
Returns:
- This object.
RemoveHeader
public Message RemoveHeader(int index)
Removes a header field by index.
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
index
- Zero-based index of the header field to set.
Returns:
- This instance.
Throws:
IllegalArgumentException
- The parameterindex
is 0 or at least as high as the number of header fields.
RemoveHeader
public Message RemoveHeader(String name)
Removes all instances of the given header field from this message. If this is a multipart message, the header field is not removed from its body part headers. A basic case-insensitive comparison is used. (Two strings are equal in such a comparison, if they match after converting the basic upper-case letters A to Z (U+0041 to U+005A) in both strings to basic lower-case letters.).
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
name
- The name of the header field to remove.
Returns:
- This instance.
Throws:
NullPointerException
- The parametername
is null.
SetBody
public Message SetBody(byte[] bytes)
Sets the body of this message to the given byte array. This method doesn't make a copy of that byte array.
Parameters:
bytes
- A byte array.
Returns:
- This object.
Throws:
NullPointerException
- The parameterbytes
is null.
SetHeader
public Message SetHeader(int index, Map.Entry<String,String> header)
Sets the name and value of a header field by index.
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
index
- Zero-based index of the header field to set.header
- A key/value pair. The key is the name of the header field, such as "From" or "Content-ID". The value is the header field's value.
Returns:
- A Message object.
Throws:
IllegalArgumentException
- The parameterindex
is 0 or at least as high as the number of header fields; or, the header field name is too long or contains an invalid character, or the header field's value is syntactically invalid.NullPointerException
- The key or value ofheader
is null.
SetHeader
public Message SetHeader(int index, String name, String value)
Sets the name and value of a header field by index.
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
index
- Zero-based index of the header field to set.name
- Name of a header field, such as "From" or "Content-ID" .value
- Value of the header field.
Returns:
- This instance.
Throws:
IllegalArgumentException
- The parameterindex
is 0 or at least as high as the number of header fields; or, the header field name is too long or contains an invalid character, or the header field's value is syntactically invalid.NullPointerException
- The parametername
orvalue
is null.
SetHeader
public Message SetHeader(int index, String value)
Sets the value of a header field by index without changing its name.
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
index
- Zero-based index of the header field to set.value
- Value of the header field.
Returns:
- This instance.
Throws:
IllegalArgumentException
- The parameterindex
is 0 or at least as high as the number of header fields; or, the header field name is too long or contains an invalid character, or the header field's value is syntactically invalid.NullPointerException
- The parametervalue
is null.
DecodeHeaderValue
public static String DecodeHeaderValue(String name, String value)
Decodes RFC 2047 encoded words from the given header field value and returns a string with those words decoded. For an example of encoded words, see the constructor for PeterO.Mail.NamedAddress.
Parameters:
name
- Name of the header field. This determines the syntax of the "value" parameter and is necessary to help this method interpret encoded words properly.value
- A header field value that could contain encoded words. For example, if the name parameter is "From", this parameter could be "=?utf-8?q?me?= <me@example.com>".
Returns:
- The header field value with valid encoded words decoded.
Throws:
NullPointerException
- The parametername
is null.
SetHeader
public Message SetHeader(String name, String value)
Sets the value of this message's header field. If a header field with the same name exists, its value is replaced. If the header field's name occurs more than once, only the first instance of the header field is replaced.
This method updates the ContentType and ContentDisposition properties if those header fields have been modified by this method.
Parameters:
name
- The name of a header field, such as "from" or "subject" .value
- The header field's value.
Returns:
- This instance.
Throws:
IllegalArgumentException
- The header field name is too long or contains an invalid character, or the header field's value is syntactically invalid.NullPointerException
- The parametername
orvalue
is null.
SetHtmlBody
public Message SetHtmlBody(String str)
Sets the body of this message to the specified string in Hypertext Markup Language (HTML) format. The character sequences CR (carriage return, "\r", U+000D), LF (line feed, "\n", U+000A), and CR/LF will be converted to CR/LF line breaks. Unpaired surrogate code points will be replaced with replacement characters.
Parameters:
str
- A string consisting of the message in HTML format.
Returns:
- This instance.
Throws:
NullPointerException
- The parameterstr
is null.
SetTextAndHtml
public Message SetTextAndHtml(String text, String html)
Sets the body of this message to a multipart body with plain text and Hypertext Markup Language (HTML) versions of the same message. The character sequences CR (carriage return, "\r", U+000D), LF (line feed, "\n", U+000A), and CR/LF will be converted to CR/LF line breaks. Unpaired surrogate code points will be replaced with replacement characters.
Parameters:
text
- A string consisting of the plain text version of the message.html
- A string consisting of the HTML version of the message.
Returns:
- This instance.
Throws:
NullPointerException
- The parametertext
orhtml
is null.
SetTextAndMarkdown
public Message SetTextAndMarkdown(String text, String markdown)
Sets the body of this message to a multipart body with plain text, Markdown, and Hypertext Markup Language (HTML) versions of the same message. The character sequences CR (carriage return, "\r", U+000D), LF (line feed, "\n", U+000A), and CR/LF will be converted to CR/LF line breaks. Unpaired surrogate code points will be replaced with replacement characters.
REMARK: The Markdown-to-HTML implementation currently supports all features of original Markdown, except that the implementation:
- does not strictly check the placement of "block-level HTML elements",
- does not prevent Markdown content from being interpreted as such merely because it's contained in a "block-level HTML element", and
- does not deliberately use HTML escapes to obfuscate email addresses wrapped in angle-brackets.
text
- A string consisting of the plain text version of the message.
Can be null, in which case the value of the "markdown" parameter is used as
the plain text version.
* markdown
- A string consisting of the Markdown version of the message.
For interoperability, this Markdown version will be converted to HTML, where
the Markdown text is assumed to be in the original Markdown flavor.
**Returns:**
* This instance.
**Throws:**
* NullPointerException
- The parameter markdown
is null.
### SetTextBody
public Message SetTextBody(String str)
Sets the body of this message to the specified plain text string. The
character sequences CR (carriage return, "\r", U+000D), LF (line feed, "\n",
U+000A), and CR/LF will be converted to CR/LF line breaks. Unpaired
surrogate code points will be replaced with replacement characters. This
method changes this message's media type to plain text.
**Parameters:**
* str
- A string consisting of the message in plain text format.
**Returns:**
* This instance.
**Throws:**
* NullPointerException
- The parameter str
is null.
### AddInline
public Message AddInline(MediaType mediaType)
Adds an inline body part with an empty body and with the given media type to
this message. Before the new body part is added, if this message isn't
already a multipart message, it becomes a "multipart/mixed" message with the
current body converted to an inline body part.
**Parameters:**
* mediaType
- A media type to assign to the body part.
**Returns:**
* A Message object for the generated body part.
### AddAttachment
public Message AddAttachment(MediaType mediaType)
Adds an attachment with an empty body and with the given media type to this
message. Before the new attachment is added, if this message isn't already a
multipart message, it becomes a "multipart/mixed" message with the current
body converted to an inline body part.
**Parameters:**
* mediaType
- A media type to assign to the attachment.
**Returns:**
* A Message object for the generated attachment.
### AddAttachment
public Message AddAttachment(InputStream inputStream, MediaType mediaType)
Adds an attachment to this message in the form of data from the given readable stream, and with the given media type. Before the new attachment is added, if this message isn't already a multipart message, it becomes a "multipart/mixed" message with the current body converted to an inline body part.
The following example (written in C# for the.NET version) is an extension method that adds an attachment from a byte array to a message.
public static Message AddAttachmentFromBytes(Message msg, byte[] bytes, MediaType mediaType) { { MemoryStream fs = null; try { fs = new MemoryStream(bytes); return msg.AddAttachment(fs, mediaType); } finally { try { if (fs != null) { fs.close(); } } catch (java.io.IOException ex) {} } } }. **Parameters:** *
inputStream
- A readable data stream.
* mediaType
- A media type to assign to the attachment.
**Returns:**
* A Message object for the generated attachment.
**Throws:**
* NullPointerException
- The parameter inputStream
or
mediaType
is null.
* MessageDataException
- An I/O error occurred.
### AddAttachment
public Message AddAttachment(InputStream inputStream, String filename)
Adds an attachment to this message in the form of data from the given
readable stream, and with the given file name. Before the new attachment is
added, if this message isn't already a multipart message, it becomes a
"multipart/mixed" message with the current body converted to an inline body
part.
**Parameters:**
* inputStream
- A readable data stream.
* filename
- A file name to assign to the attachment. Can be null or
empty, in which case no file name is assigned. Only the file name portion of
this parameter is used, which in this case means the portion of the string
after the last "/" or "\", if either character exists, or the entire string
otherwise An appropriate media type (or "application/octet-stream") will be
assigned to the attachment based on this file name's extension. If the file
name has an extension .txt, .text, .htm, .html, .shtml, .asc, .brf, .pot,
.rst, .md, .markdown, or .srt, the media type will have a "charset" of
"utf-8".
**Returns:**
* A Message object for the generated attachment.
**Throws:**
* NullPointerException
- The parameter inputStream
is null.
* MessageDataException
- An I/O error occurred.
### AddAttachment
public Message AddAttachment(InputStream inputStream, MediaType mediaType, String filename)
Adds an attachment to this message in the form of data from the given
readable stream, and with the given media type and file name. Before the new
attachment is added, if this message isn't already a multipart message, it
becomes a "multipart/mixed" message with the current body converted to an
inline body part.
**Parameters:**
* inputStream
- A readable data stream.
* mediaType
- A media type to assign to the attachment.
* filename
- A file name to assign to the attachment. Can be null or
empty, in which case no file name is assigned. Only the file name portion of
this parameter is used, which in this case means the portion of the string
after the last "/" or "\", if either character exists, or the entire string
otherwise.
**Returns:**
* A Message object for the generated attachment.
**Throws:**
* NullPointerException
- The parameter inputStream
or
mediaType
is null.
* MessageDataException
- An I/O error occurred.
### AddInline
public Message AddInline(InputStream inputStream, MediaType mediaType)
Adds an inline body part to this message in the form of data from the given readable stream, and with the given media type. Before the new body part is added, if this message isn't already a multipart message, it becomes a "multipart/mixed" message with the current body converted to an inline body part.
The following example (written in C# for the.NET version) is an extension method that adds an inline body part from a byte array to a message.
public static Message AddInlineFromBytes(Message msg, byte[] bytes, MediaType mediaType) { { MemoryStream fs = null; try { fs = new MemoryStream(bytes); return msg.AddInline(fs, mediaType); } finally { try { if (fs != null) { fs.close(); } } catch (java.io.IOException ex) {} } } }. **Parameters:** *
inputStream
- A readable data stream.
* mediaType
- A media type to assign to the body part.
**Returns:**
* A Message object for the generated body part.
**Throws:**
* NullPointerException
- The parameter inputStream
or
mediaType
is null.
* MessageDataException
- An I/O error occurred.
### AddInline
public Message AddInline(InputStream inputStream, String filename)
Adds an inline body part to this message in the form of data from the given
readable stream, and with the given file name. Before the new body part is
added, if this message isn't already a multipart message, it becomes a
"multipart/mixed" message with the current body converted to an inline body
part.
**Parameters:**
* inputStream
- A readable data stream.
* filename
- A file name to assign to the inline body part. Can be null
or empty, in which case no file name is assigned. Only the file name portion
of this parameter is used, which in this case means the portion of the
string after the last "/" or "\", if either character exists, or the entire
string otherwise An appropriate media type (or "application/octet-stream")
will be assigned to the body part based on this file name's extension. If
the file name has an extension .txt, .text, .htm, .html, .shtml, .asc, .brf,
.pot, .rst, .md, .markdown, or .srt, the media type will have a "charset" of
"utf-8".
**Returns:**
* A Message object for the generated body part.
**Throws:**
* NullPointerException
- The parameter inputStream
or
"mediaType" is null.
* MessageDataException
- An I/O error occurred.
### AddInline
public Message AddInline(InputStream inputStream, MediaType mediaType, String filename)
Adds an inline body part to this message in the form of data from the given
readable stream, and with the given media type and file name. Before the new
body part is added, if this message isn't already a multipart message, it
becomes a "multipart/mixed" message with the current body converted to an
inline body part.
**Parameters:**
* inputStream
- A readable data stream.
* mediaType
- A media type to assign to the body part.
* filename
- A file name to assign to the body part.
**Returns:**
* A Message object for the generated body part.
**Throws:**
* NullPointerException
- The parameter inputStream
or
mediaType
is null.
* MessageDataException
- An I/O error occurred.
### SelectLanguageMessage
public Message SelectLanguageMessage(List
multipart/multilingual
) according to the given language priority list.
**Parameters:**
* languages
- A list of basic language ranges, sorted in descending order
of priority (see the LanguageTags.LanguageTagFilter method).
**Returns:**
* The best matching body part for the given languages. If the body
part has no subject, then the top-level subject is used. If this message is
not a multipart/multilingual message or has fewer than two body parts,
returns this object. If no body part matches the given languages, returns
the last body part if its language is "zxx", or the second body part
otherwise.
**Throws:**
* NullPointerException
- The parameter languages
is null.
### SelectLanguageMessage
public Message SelectLanguageMessage(List
multipart/multilingual
) according to the given language priority list and
original-language preference.
**Parameters:**
* languages
- A list of basic language ranges, sorted in descending order
of priority (see the LanguageTags.LanguageTagFilter method).
* preferOriginals
- If true, a body part marked as the original language
version is chosen if it matches one of the given language ranges, even if
the original language has a lower priority than another language with a
matching body part.
**Returns:**
* The best matching body part for the given languages. If the body
part has no subject, then the top-level subject is used. If this message is
not a multipart/multilingual message or has fewer than two body parts,
returns this object. If no body part matches the given languages, returns
the last body part if its language is "zxx", or the second body part
otherwise.
**Throws:**
* NullPointerException
- The parameter languages
is null.
### MakeMultilingualMessage
public static Message MakeMultilingualMessage(Listmessages
- A list of messages forming the parts of the multilingual
message object. Each message should have the same content, but be in a
different language. Each message must have a From header field and use the
same email address in that field as the other messages. The messages should
be ordered in descending preference of language.
* languages
- A list of language strings corresponding to the messages
given in the "messages" parameter. A language string at a given index
corresponds to the message at the same index. Each language string must
follow the syntax of the Content-Language header field (see
LanguageTags.GetLanguageList).
**Returns:**
* A Message object with the content type "multipart/multilingual" . It
will begin with an explanatory body part and be followed by the messages
given in the messages
parameter in the order given.
**Throws:**
* NullPointerException
- The parameter messages
or
languages
is null.
* IllegalArgumentException
- The parameter messages
or
languages
is empty, their lengths don't match, at least one message is
"null", each message doesn't contain the same email addresses in their From
header fields, languages
contains a syntactically invalid language
tag list, languages
contains the language tag "zzx" not appearing
alone or at the end of the language tag list, or the first message contains
no From header field.
### FromMailtoUrl
@Deprecated public static Message FromMailtoUrl(String url)
Creates a message object from a MailTo URI (uniform resource identifier). For more information, see FromMailtoUri(string).
Parameters:
url
- A MailTo URI.
Returns:
- A Message object created from the given MailTo URI. Returs null if
url
is null, is syntactically invalid, or is not a MailTo URI.
ToMailtoUrl
@Deprecated public String ToMailtoUrl()
Generates a MailTo URI (uniform resource identifier) corresponding to this
message. The following header fields, and only these, are used to generate
the URI: To, Cc, Bcc, In-Reply-To, Subject, Keywords, Comments. The message
body is included in the URI only if GetBodyString()
would return a
non-empty string. The To header field is included in the URI only if it has
display names or group syntax.
Returns:
- A MailTo URI corresponding to this message.
FromMailtoUri
public static Message FromMailtoUri(String uri)
Creates a message object from a MailTo URI (uniform resource identifier). The MailTo URI can contain key-value pairs that follow a question-mark, as in the following example: "mailto:me@example.com?subject=A%20Subject". In this example, "subject" is the subject of the email address. Only certain keys are supported, namely, "to", "cc", "bcc", "subject", "in-reply-to", "comments", "keywords", and "body". The first seven are header field names that will be used to set the returned message's corresponding header fields. The last, "body", sets the body of the message to the given text. Keys other than these eight will be ignored. (Keys are compared using a basic case-sensitive comparison, in which two strings are equal if they match after converting the basic upper-case letters A to Z (U+0041 to U+005A) in both strings to basic lower-case letters.) The same key (matched using a basic case-insensitive comparison) can appear more than once; for "subject", "cc", "bcc", and "in-reply-to", the last value with the given key is used; for "to", all header field values as well as the path are combined to a single To header field; for "keywords" and "comments", each value adds another header field of the given key; and for "body", the last value with that key is used as the body.
Parameters:
uri
- The parameteruri
is a text string.
Returns:
- A Message object created from the given MailTo URI. Returs null if
uri
is null, is syntactically invalid, or is not a MailTo URI.
FromMailtoUri
public static Message FromMailtoUri(URI uri)
Creates a message object from a MailTo URI (uniform resource identifier) in the form of a URI object. For more information, see FromMailtoUri(string).
Parameters:
uri
- The MailTo URI in the form of a URI object.
Returns:
- A Message object created from the given MailTo URI. Returs null if
uri
is null, is syntactically invalid, or is not a MailTo URI.
Throws:
NullPointerException
- The parameteruri
is null.
ToMailtoUri
public String ToMailtoUri()
Generates a MailTo URI (uniform resource identifier) corresponding to this
message. The following header fields, and only these, are used to generate
the URI: To, Cc, Bcc, In-Reply-To, Subject, Keywords, Comments. The message
body is included in the URI only if GetBodyString()
would return a
non-empty string.. The To header field is included in the URI only if it has
display names or group syntax.
Returns:
- A MailTo URI corresponding to this message.