/// <summary> /// Constructs a message from a byte array.<br/> /// <br/> /// The headers are always parsed, but if <paramref name="parseBody"/> is <see langword="false"/>, the body is not parsed. /// </summary> /// <param name="rawMessageContent">The byte array which is the message contents to parse</param> /// <param name="parseBody"> /// <see langword="true"/> if the body should be parsed, /// <see langword="false"/> if only headers should be parsed out of the <paramref name="rawMessageContent"/> byte array /// </param> public Message(byte[] rawMessageContent, bool parseBody) { RawMessage = rawMessageContent; // Find the headers and the body parts of the byte array MessageHeader headersTemp; byte[] body; HeaderExtractor.ExtractHeadersAndBody(rawMessageContent, out headersTemp, out body); // Set the Headers property Headers = headersTemp; // Should we also parse the body? if (parseBody) { // Parse the body into a MessagePart MessagePart = new MessagePart(body, Headers); } }
/// <summary> /// This method will convert this <see cref="Message"/> into a <see cref="MailMessage"/> equivalent.<br/> /// The returned <see cref="MailMessage"/> can be used with <see cref="System.Net.Mail.SmtpClient"/> to forward the email.<br/> /// <br/> /// You should be aware of the following about this method: /// <list type="bullet"> /// <item> /// All sender and receiver mail addresses are set. /// If you send this email using a <see cref="System.Net.Mail.SmtpClient"/> then all /// receivers in To, From, Cc and Bcc will receive the email once again. /// </item> /// <item> /// If you view the source code of this Message and looks at the source code of the forwarded /// <see cref="MailMessage"/> returned by this method, you will notice that the source codes are not the same. /// The content that is presented by a mail client reading the forwarded <see cref="MailMessage"/> should be the /// same as the original, though. /// </item> /// <item> /// Content-Disposition headers will not be copied to the <see cref="MailMessage"/>. /// It is simply not possible to set these on Attachments. /// </item> /// <item> /// HTML content will be treated as the preferred view for the <see cref="MailMessage.Body"/>. Plain text content will be used for the /// <see cref="MailMessage.Body"/> when HTML is not available. /// </item> /// </list> /// </summary> /// <returns>A <see cref="MailMessage"/> object that contains the same information that this Message does</returns> public MailMessage ToMailMessage() { // Construct an empty MailMessage to which we will gradually build up to look like the current Message object (this) MailMessage message = new MailMessage(); message.Subject = Headers.Subject; // We here set the encoding to be UTF-8 // We cannot determine what the encoding of the subject was at this point. // But since we know that strings in .NET is stored in UTF, we can // use UTF-8 to decode the subject into bytes message.SubjectEncoding = Encoding.UTF8; // The HTML version should take precedent over the plain text if it is available MessagePart preferredVersion = FindFirstHtmlVersion(); if (preferredVersion != null) { // Make sure that the IsBodyHtml property is being set correctly for our content message.IsBodyHtml = true; } else { // otherwise use the first plain text version as the body, if it exists preferredVersion = FindFirstPlainTextVersion(); } if (preferredVersion != null) { message.Body = preferredVersion.GetBodyAsText(); message.BodyEncoding = preferredVersion.BodyEncoding; } // Add body and alternative views (html and such) to the message IEnumerable <MessagePart> textVersions = FindAllTextVersions(); foreach (MessagePart textVersion in textVersions) { // The textVersions also contain the preferred version, therefore // we should skip that one if (textVersion == preferredVersion) { continue; } MemoryStream stream = new MemoryStream(textVersion.Body); AlternateView alternative = new AlternateView(stream); alternative.ContentId = textVersion.ContentId; alternative.ContentType = textVersion.ContentType; message.AlternateViews.Add(alternative); } // Add attachments to the message IEnumerable <MessagePart> attachments = FindAllAttachments(); foreach (MessagePart attachmentMessagePart in attachments) { MemoryStream stream = new MemoryStream(attachmentMessagePart.Body); Attachment attachment = new Attachment(stream, attachmentMessagePart.ContentType); attachment.ContentId = attachmentMessagePart.ContentId; message.Attachments.Add(attachment); } if (Headers.From != null && Headers.From.HasValidMailAddress) { message.From = Headers.From.MailAddress; } if (Headers.ReplyTo != null && Headers.ReplyTo.HasValidMailAddress) { message.ReplyTo = Headers.ReplyTo.MailAddress; } if (Headers.Sender != null && Headers.Sender.HasValidMailAddress) { message.Sender = Headers.Sender.MailAddress; } foreach (RfcMailAddress to in Headers.To) { if (to.HasValidMailAddress) { message.To.Add(to.MailAddress); } } foreach (RfcMailAddress cc in Headers.Cc) { if (cc.HasValidMailAddress) { message.CC.Add(cc.MailAddress); } } foreach (RfcMailAddress bcc in Headers.Bcc) { if (bcc.HasValidMailAddress) { message.Bcc.Add(bcc.MailAddress); } } return(message); }