PeterO.Mail.Message
## PeterO.Mail.Message
public sealed class Message
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.
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.
Member Summary
[AddAttachment(PeterO.Mail.MediaType)](#AddAttachment_PeterO_Mail_MediaType)
- Adds an attachment with an empty body and with the given media type to this message.[AddAttachment(System.IO.Stream, PeterO.Mail.MediaType)](#AddAttachment_System_IO_Stream_PeterO_Mail_MediaType)
- Adds an attachment to this message in the form of data from the given readable stream, and with the given media type.[AddAttachment(System.IO.Stream, PeterO.Mail.MediaType, string)](#AddAttachment_System_IO_Stream_PeterO_Mail_MediaType_string)
- 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.[AddAttachment(System.IO.Stream, string)](#AddAttachment_System_IO_Stream_string)
- Adds an attachment to this message in the form of data from the given readable stream, and with the given file name.[AddHeader(string, string)](#AddHeader_string_string)
- Adds a header field to the end of the message’s header.[AddHeader(System.Collections.Generic.KeyValuePair)](#AddHeader_System_Collections_Generic_KeyValuePair)
- Adds a header field to the end of the message’s header.[AddInline(PeterO.Mail.MediaType)](#AddInline_PeterO_Mail_MediaType)
- Adds an inline body part with an empty body and with the given media type to this message.[AddInline(System.IO.Stream, PeterO.Mail.MediaType)](#AddInline_System_IO_Stream_PeterO_Mail_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.[AddInline(System.IO.Stream, PeterO.Mail.MediaType, string)](#AddInline_System_IO_Stream_PeterO_Mail_MediaType_string)
- 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.[AddInline(System.IO.Stream, string)](#AddInline_System_IO_Stream_string)
- Adds an inline body part to this message in the form of data from the given readable stream, and with the given file name.[BccAddresses](#BccAddresses)
- Deprecated: Use GetAddresses("Bcc") instead.[BodyString](#BodyString)
- Deprecated: Use GetBodyString() instead.[CCAddresses](#CCAddresses)
- Deprecated: Use GetAddresses("Cc") instead.[ClearHeaders()](#ClearHeaders)
- Deletes all header fields in this message.[ContentDisposition](#ContentDisposition)
- Gets or sets this message’s content disposition.[ContentType](#ContentType)
- Gets or sets this message’s media type.[DecodeHeaderValue(string, string)](#DecodeHeaderValue_string_string)
- Decodes RFC 2047 encoded words from the given header field value and returns a string with those words decoded.[ExtractHeader(byte[], string)](#ExtractHeader_byte_string)
- Extracts the value of a header field from a byte array representing an email message.[FileName](#FileName)
- Gets a file name suggested by this message for saving the message’s body to a file.[FromAddresses](#FromAddresses)
- Deprecated: Use GetAddresses("From") instead.[FromMailtoUri(string)](#FromMailtoUri_string)
- Creates a message object from a MailTo URI (uniform resource identifier).[FromMailtoUri(System.Uri)](#FromMailtoUri_System_Uri)
- Creates a message object from a MailTo URI (uniform resource identifier) in the form of a URI object.[FromMailtoUrl(string)](#FromMailtoUrl_string)
- Deprecated: Renamed to FromMailtoUri.[Generate()](#Generate)
- Generates this message’s data in text form.[GenerateBytes()](#GenerateBytes)
- Generates this message’s data as a byte array, using the same algorithm as the Generate method.[GetAddresses(string)](#GetAddresses_string)
- Gets a list of addresses contained in the header fields with the given name in this message.[GetAttachments()](#GetAttachments)
- Gets a list of descendant body parts of this message that are considered attachments.[GetBody()](#GetBody)
- Gets the byte array for this message’s body.[GetBodyMessage()](#GetBodyMessage)
- Returns the mail message contained in this message’s body.[GetBodyString()](#GetBodyString)
- Gets the body of this message as a text string.[GetDate()](#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).[GetFormattedBodyString()](#GetFormattedBodyString)
- Gets a Hypertext Markup Language (HTML) rendering of this message’s text body.[GetHeader(int)](#GetHeader_int)
- Gets the name and value of a header field by index.[GetHeader(string)](#GetHeader_string)
- Gets the first instance of the header field with the specified name, using a basic case-insensitive comparison.[GetHeaderArray(string)](#GetHeaderArray_string)
- Gets an array with the values of all header fields with the specified name, using a basic case-insensitive comparison.[HeaderFields](#HeaderFields)
- Gets a snapshot of the header fields of this message, in the order in which they appear in the message.[MakeMultilingualMessage(System.Collections.Generic.IList, System.Collections.Generic.IList)](#MakeMultilingualMessage_System_Collections_Generic_IList_System_Collections_Generic_IList)
- Generates a multilingual message (see RFC 8255) from a list of messages and a list of language strings.[NewBodyPart()](#NewBodyPart)
- Creates a message object with no header fields.[Parts](#Parts)
- Gets a list of all the parts of this message.[RemoveHeader(int)](#RemoveHeader_int)
- Removes a header field by index.[RemoveHeader(string)](#RemoveHeader_string)
- Removes all instances of the given header field from this message.[SelectLanguageMessage(System.Collections.Generic.IList)](#SelectLanguageMessage_System_Collections_Generic_IList)
- Selects a body part for a multiple-language message( multipart/multilingual ) according to the given language priority list.[SelectLanguageMessage(System.Collections.Generic.IList, bool)](#SelectLanguageMessage_System_Collections_Generic_IList_bool)
- Selects a body part for a multiple-language message( multipart/multilingual ) according to the given language priority list and original-language preference.[SetBody(byte[])](#SetBody_byte)
- Sets the body of this message to the given byte array.[SetCurrentDate()](#SetCurrentDate)
- Sets this message’s Date header field to the current time as its value, with an unspecified time zone offset.[SetDate(int[])](#SetDate_int)
- Sets this message’s Date header field to the given date and time.[SetHeader(int, string)](#SetHeader_int_string)
- Sets the value of a header field by index without changing its name.[SetHeader(int, string, string)](#SetHeader_int_string_string)
- Sets the name and value of a header field by index.[SetHeader(int, System.Collections.Generic.KeyValuePair)](#SetHeader_int_System_Collections_Generic_KeyValuePair)
- Sets the name and value of a header field by index.[SetHeader(string, string)](#SetHeader_string_string)
- Sets the value of this message’s header field.[SetHtmlBody(string)](#SetHtmlBody_string)
- Sets the body of this message to the specified string in Hypertext Markup Language (HTML) format.[SetTextAndHtml(string, string)](#SetTextAndHtml_string_string)
- Sets the body of this message to a multipart body with plain text and Hypertext Markup Language (HTML) versions of the same message.[SetTextAndMarkdown(string, string)](#SetTextAndMarkdown_string_string)
- Sets the body of this message to a multipart body with plain text, Markdown, and Hypertext Markup Language (HTML) versions of the same message.[SetTextBody(string)](#SetTextBody_string)
- Sets the body of this message to the specified plain text string.[Subject](#Subject)
- Gets or sets this message’s subject.[ToAddresses](#ToAddresses)
- Deprecated: Use GetAddresses("To") instead.[ToMailtoUri()](#ToMailtoUri)
- Generates a MailTo URI (uniform resource identifier) corresponding to this message.[ToMailtoUrl()](#ToMailtoUrl)
- Deprecated: Renamed to ToMailtoUri.
public Message( byte[] bytes);
Initializes a new instance of the PeterO.Mail.Message class. Reads from the given byte array to initialize the email message.
Remarks:This constructor parses an email message, and extracts its header fields and body, and throws a MessageDataException if the message is malformed. However, even if a MessageDataException is thrown, it can still be possible to display the message, especially because most email malformations seen in practice are benign in nature (such as the use of very long lines in the message). One way an application can handle the exception is to display the message, or part of it, as raw text (using DataUtilities.GetUtf8String(bytes, true)
), and to optionally extract important header fields, such as From, To, Date, and Subject, from the message’s text using the ExtractHeader
method. Even so, though, any message for which this constructor throws a MessageDataException ought to be treated with suspicion.
Parameters:
- bytes: A readable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter bytes is null.
-
PeterO.Mail.MessageDataException: The message is malformed. See the remarks.
public Message( System.IO.Stream stream);
Initializes a new instance of the PeterO.Mail.Message class. Reads from the given Stream object to initialize the email message.
Remarks:This constructor parses an email message, and extracts its header fields and body, and throws a MessageDataException if the message is malformed. However, even if a MessageDataException is thrown, it can still be possible to display the message, especially because most email malformations seen in practice are benign in nature (such as the use of very long lines in the message). One way an application can handle the exception is to read all the bytes from the stream, to display the message, or part of it, as raw text (using DataUtilities.GetUtf8String(bytes, true)
), and to optionally extract important header fields, such as From, To, Date, and Subject, from the message’s text using the ExtractHeader
method. Even so, though, any message for which this constructor throws a MessageDataException ought to be treated with suspicion.
Parameters:
- stream: A readable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
PeterO.Mail.MessageDataException: The message is malformed. See the remarks.
public Message();
Initializes a new instance of the PeterO.Mail.Message class. The message will be plain text and have an artificial From address.
public System.Collections.Generic.IList BccAddresses { get; }
Deprecated. Use GetAddresses(“Bcc”) instead.
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.
public string BodyString { get; }
Deprecated. Use GetBodyString() instead.
Gets the body of this message as a text string. See the GetBodyString()
method.
Returns:
The body of this message as a text string.
Exceptions:
- System.NotSupportedException:
See the
GetBodyString()
method.
public System.Collections.Generic.IList CCAddresses { get; }
Deprecated. Use GetAddresses(“Cc”) instead.
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.
public PeterO.Mail.ContentDisposition ContentDisposition { get; set; }
Gets or sets 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.
public PeterO.Mail.MediaType ContentType { get; set; }
Gets or sets 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.
Exceptions:
- System.ArgumentNullException: This value is being set and “value” is null.
public string FileName { get; }
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.
public System.Collections.Generic.IList FromAddresses { get; }
Deprecated. Use GetAddresses(“From”) instead.
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.
public System.Collections.Generic.IList HeaderFields { get; }
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.
public System.Collections.Generic.IList Parts { get; }
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.
public string Subject { get; set; }
Gets or sets 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.
public System.Collections.Generic.IList ToAddresses { get; }
Deprecated. Use GetAddresses(“To”) instead.
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.
public PeterO.Mail.Message AddAttachment( PeterO.Mail.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.
Return Value:
A Message object for the generated attachment.
public PeterO.Mail.Message AddAttachment( System.IO.Stream inputStream, PeterO.Mail.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(this Message msg, byte[] bytes, MediaType mediaType) { using (var fs = new MemoryStream(bytes)) { return msg.AddAttachment(fs, mediaType); } }
.
Parameters:
-
inputStream: A readable data stream.
-
mediaType: A media type to assign to the attachment.
Return Value:
A Message object for the generated attachment.
Exceptions:
-
System.ArgumentNullException: The parameter inputStream or mediaType is null.
-
PeterO.Mail.MessageDataException: An I/O error occurred.
public PeterO.Mail.Message AddAttachment( System.IO.Stream inputStream, PeterO.Mail.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.
Return Value:
A Message object for the generated attachment.
Exceptions:
-
System.ArgumentNullException: The parameter inputStream or mediaType is null.
-
PeterO.Mail.MessageDataException: An I/O error occurred.
public PeterO.Mail.Message AddAttachment( System.IO.Stream 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”.
Return Value:
A Message object for the generated attachment.
Exceptions:
-
System.ArgumentNullException: The parameter inputStream is null.
-
PeterO.Mail.MessageDataException: An I/O error occurred.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
-
System.ArgumentNullException: The parameter name or value is null.
-
System.ArgumentException: The header field name is too long or contains an invalid character, or the header field’s value is syntactically invalid.
public PeterO.Mail.Message AddHeader( System.Collections.Generic.KeyValuePair 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.
Return Value:
This instance.
Exceptions:
-
System.ArgumentNullException: The key or value of header is null.
-
System.ArgumentException: The header field name is too long or contains an invalid character, or the header field’s value is syntactically invalid.
public PeterO.Mail.Message AddInline( PeterO.Mail.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.
Return Value:
A Message object for the generated body part.
public PeterO.Mail.Message AddInline( System.IO.Stream inputStream, PeterO.Mail.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(this Message msg, byte[] bytes, MediaType mediaType) { using (MemoryStream fs = new MemoryStream(bytes)) { return msg.AddInline(fs, mediaType); } }
.
Parameters:
-
inputStream: A readable data stream.
-
mediaType: A media type to assign to the body part.
Return Value:
A Message object for the generated body part.
Exceptions:
-
System.ArgumentNullException: The parameter inputStream or mediaType is null.
-
PeterO.Mail.MessageDataException: An I/O error occurred.
public PeterO.Mail.Message AddInline( System.IO.Stream inputStream, PeterO.Mail.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.
Return Value:
A Message object for the generated body part.
Exceptions:
-
System.ArgumentNullException: The parameter inputStream or mediaType is null.
-
PeterO.Mail.MessageDataException: An I/O error occurred.
public PeterO.Mail.Message AddInline( System.IO.Stream 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”.
Return Value:
A Message object for the generated body part.
Exceptions:
-
System.ArgumentNullException: The parameter inputStream or “mediaType” is null.
-
PeterO.Mail.MessageDataException: An I/O error occurred.
public PeterO.Mail.Message ClearHeaders();
Deletes all header fields in this message. Also clears this message’s content disposition and resets its content type to MediaType.TextPlainAscii.
Return Value:
This object.
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”.
Return Value:
The header field value with valid encoded words decoded.
Exceptions:
- System.ArgumentNullException: The parameter name is null.
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.).
Return Value:
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, if headerFieldName 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.
public static PeterO.Mail.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 parameter uri is a text string.
Return Value:
A Message object created from the given MailTo URI. Returs null if uri is null, is syntactically invalid, or is not a MailTo URI.
public static PeterO.Mail.Message FromMailtoUri( System.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.
Return Value:
A Message object created from the given MailTo URI. Returs null if uri is null, is syntactically invalid, or is not a MailTo URI.
Exceptions:
- System.ArgumentNullException: The parameter uri is null.
public static PeterO.Mail.Message FromMailtoUrl( string url);
Deprecated. Renamed to FromMailtoUri.
Creates a message object from a MailTo URI (uniform resource identifier). For more information, see FromMailtoUri(string).
Parameters:
- url: A MailTo URI.
Return Value:
A Message object created from the given MailTo URI. Returs null if url is null, is syntactically invalid, or is not a MailTo URI.
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.
Return Value:
The generated message.
Exceptions:
- PeterO.Mail.MessageDataException: The message can’t be generated.
public byte[] GenerateBytes();
Generates this message’s data as a byte array, using the same algorithm as the Generate method.
Return Value:
The generated message as a byte array.
public System.Collections.Generic.IList 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.
Return Value:
A list of addresses, in the order in which they appear in this message’s header fields of the given name.
Exceptions:
-
System.NotSupportedException: The parameter headerName is not supported for this method. Currently, the only header fields supported are To, Cc, Bcc, Reply-To, Sender, and From.
-
System.ArgumentNullException: The parameter headerName is null.
-
System.ArgumentException: The parameter headerName is empty.
public System.Collections.Generic.IList 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.
Return Value:
A list of descendant body parts of this message that are considered attachments.
public byte[] GetBody();
Gets the byte array for this message’s body. This method doesn’ t make a copy of that byte array.
Return Value:
A byte array.
public PeterO.Mail.Message GetBodyMessage();
Returns the mail message contained in this message’s body.
Return Value:
A message object if this object’s content type is “message/rfc822”, “message/news”, or “message/global”, or null otherwise.
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.
Return Value:
The body of this message as a text string.
Exceptions:
- System.NotSupportedException: 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.
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, bool) for more information on the format of the date-time array returned by this method.
Return Value:
An array of 32-bit unsigned integers.
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.
Return Value:
An HTML rendering of this message’s text.
Exceptions:
- System.NotSupportedException:
No supported body part was found; see
GetBodyString()
for more information.
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.
Return Value:
The value of the first header field with that name, or null if there is none.
Exceptions:
- System.ArgumentNullException: Name is null.
public System.Collections.Generic.KeyValuePair 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.
Return Value:
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.
Exceptions:
- System.ArgumentException: The parameter index is 0 or at least as high as the number of header fields.
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.
Return Value:
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.
Exceptions:
- System.ArgumentNullException: Name is null.
public static PeterO.Mail.Message MakeMultilingualMessage( System.Collections.Generic.IList messages, System.Collections.Generic.IList languages);
Generates a multilingual message (see RFC 8255) from a list of messages and a list of language strings.
Parameters:
-
messages: 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).
Return Value:
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.
Exceptions:
-
System.ArgumentNullException: The parameter messages or languages is null.
-
System.ArgumentException: 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.
public static PeterO.Mail.Message NewBodyPart();
Creates a message object with no header fields.
Return Value:
A message object with no header fields.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
- System.ArgumentException: The parameter index is 0 or at least as high as the number of header fields.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
- System.ArgumentNullException: The parameter name is null.
public PeterO.Mail.Message SelectLanguageMessage( System.Collections.Generic.IList languages);
Selects a body part for a multiple-language message( 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).
Return Value:
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.
Exceptions:
- System.ArgumentNullException: The parameter languages is null.
public PeterO.Mail.Message SelectLanguageMessage( System.Collections.Generic.IList languages, bool preferOriginals);
Selects a body part for a multiple-language message( 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.
Return Value:
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.
Exceptions:
- System.ArgumentNullException: The parameter languages is null.
public PeterO.Mail.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.
Return Value:
This object.
Exceptions:
- System.ArgumentNullException: The parameter bytes is null.
public PeterO.Mail.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.
Return Value:
This object.
public PeterO.Mail.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, bool) for more information on this parameter.
Return Value:
This object.
Exceptions:
-
System.ArgumentException: The parameter dateTime contains fewer than eight elements or contains invalid values (see MailDateTime.ParseDateString(string, bool) ).
-
System.ArgumentNullException: The parameter dateTime is null.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
-
System.ArgumentException: The parameter index 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.
-
System.ArgumentNullException: The parameter name or value is null.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
-
System.ArgumentException: The parameter index 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.
-
System.ArgumentNullException: The parameter value is null.
public PeterO.Mail.Message SetHeader( int index, System.Collections.Generic.KeyValuePair 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.
Return Value:
A Message object.
Exceptions:
-
System.ArgumentException: The parameter index 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.
-
System.ArgumentNullException: The key or value of header is null.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
-
System.ArgumentException: The header field name is too long or contains an invalid character, or the header field’s value is syntactically invalid.
-
System.ArgumentNullException: The parameter name or value is null.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
- System.ArgumentNullException: The parameter str is null.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
- System.ArgumentNullException: The parameter text or html is null.
public PeterO.Mail.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.
Parameters:
-
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.
Return Value:
This instance.
Exceptions:
- System.ArgumentNullException: The parameter markdown is null.
public PeterO.Mail.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.
Return Value:
This instance.
Exceptions:
- System.ArgumentNullException: The parameter str is null.
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.
Return Value:
A MailTo URI corresponding to this message.
public string ToMailtoUrl();
Deprecated. Renamed to 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.
Return Value:
A MailTo URI corresponding to this message.