using System; using System.Collections.Generic; using System.Net.Mail; using System.Net.Mime; using System.Text.RegularExpressions; namespace Ant.Service.Utilities { /// /// This class adds a few internet mail headers not already exposed by the /// System.Net.MailMessage. It also provides support to encapsulate the /// nested mail attachments in the Children collection. /// public class MailMessageEx : System.Net.Mail.MailMessage { public const string EmailRegexPattern = "(['\"]{1,}.+['\"]{1,}\\s+)??"; private long _octets; public long Octets { get { return _octets; } set { _octets = value; } } private int _messageNumber; /// /// Gets or sets the message number of the MailMessage on the POP3 server. /// /// The message number. public int MessageNumber { get { return _messageNumber; } internal set { _messageNumber = value; } } private static readonly char[] AddressDelimiters = new char[] { ',', ';' }; private List _children; /// /// Gets the children MailMessage attachments. /// /// The children MailMessage attachments. public List Children { get { return _children; } } /// /// Gets the delivery date. /// /// The delivery date. public DateTime DeliveryDate { get { string date = GetHeader(MailHeaders.Date); if (string.IsNullOrEmpty(date)) { return DateTime.MinValue; } if ((date.IndexOf("(EST)") > 1) && (date.IndexOf("-") > 1)) { date = date.Substring(0, date.IndexOf("-")); } return Convert.ToDateTime(date); } } /// /// Gets the return address. /// /// The return address. public MailAddress ReturnAddress { get { string replyTo = GetHeader(MailHeaders.ReplyTo); if (string.IsNullOrEmpty(replyTo)) { return null; } return CreateMailAddress(replyTo); } } /// /// Gets the routing. /// /// The routing. public string Routing { get { return GetHeader(MailHeaders.Received); } } /// /// Gets the message id. /// /// The message id. public string MessageId { get { return GetHeader(MailHeaders.MessageId); } } public string ReplyToMessageId { get { return GetHeader(MailHeaders.InReplyTo, true); } } /// /// Gets the MIME version. /// /// The MIME version. public string MimeVersion { get { return GetHeader(MimeHeaders.MimeVersion); } } /// /// Gets the content id. /// /// The content id. public string ContentId { get { return GetHeader(MimeHeaders.ContentId); } } /// /// Gets the content description. /// /// The content description. public string ContentDescription { get { return GetHeader(MimeHeaders.ContentDescription); } } /// /// Gets the content disposition. /// /// The content disposition. public ContentDisposition ContentDisposition { get { string contentDisposition = GetHeader(MimeHeaders.ContentDisposition); if (string.IsNullOrEmpty(contentDisposition)) { return null; } return new ContentDisposition(contentDisposition); } } /// /// Gets the type of the content. /// /// The type of the content. public ContentType ContentType { get { string contentType = GetHeader(MimeHeaders.ContentType); if (string.IsNullOrEmpty(contentType)) { return null; } return MimeReader.GetContentType(contentType); } } /// /// Initializes a new instance of the class. /// public MailMessageEx() : base() { _children = new List(); } /// /// Gets the header. /// /// The header. /// private string GetHeader(string header) { return GetHeader(header, false); } private string GetHeader(string header, bool stripBrackets) { if (stripBrackets) { return MimeEntity.TrimBrackets(Headers[header]); } return Headers[header]; } /// /// Creates the mail message from entity. /// /// The entity. /// public static MailMessageEx CreateMailMessageFromEntity(MimeEntity entity) { MailMessageEx message = new MailMessageEx(); string value; foreach (string key in entity.Headers.AllKeys) { value = entity.Headers[key]; if (value.Equals(string.Empty)) { value = " "; } message.Headers.Add(key.ToLowerInvariant(), value); switch (key.ToLowerInvariant()) { case MailHeaders.Bcc: MailMessageEx.PopulateAddressList(value, message.Bcc); break; case MailHeaders.Cc: MailMessageEx.PopulateAddressList(value, message.CC); break; case MailHeaders.From: message.From = MailMessageEx.CreateMailAddress(value); break; case MailHeaders.ReplyTo: message.ReplyTo = MailMessageEx.CreateMailAddress(value); break; case MailHeaders.Subject: message.Subject = value; break; case MailHeaders.To: MailMessageEx.PopulateAddressList(value, message.To); break; } } return message; } /// /// Creates the mail address. /// /// The address. /// public static MailAddress CreateMailAddress(string address) { try { return new MailAddress(address.Trim('\t')); } catch //(FormatException e) { //throw new Pop3Exception("Unable to create mail address from provided string: " + address, e);Mail Delivery System return new MailAddress(address + "@mail.error"); } } /// /// Populates the address list. /// /// The address list. /// The recipients. public static void PopulateAddressList(string addressList, MailAddressCollection recipients) { foreach (MailAddress address in GetMailAddresses(addressList)) { recipients.Add(address); } } /// /// Gets the mail addresses. /// /// The address list. /// private static IEnumerable GetMailAddresses(string addressList) { Regex email = new Regex(EmailRegexPattern); foreach (Match match in email.Matches(addressList)) { yield return CreateMailAddress(match.Value); } /* string[] addresses = addressList.Split(AddressDelimiters); foreach (string address in addresses) { yield return CreateMailAddress(address); }*/ } } }