/// <summary> /// This method will convert this <see cref="MailMessage"/> 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> /// </list> /// </summary> /// <returns>A <see cref="MailMessage"/> object that contains the same information that this Message does</returns> public System.Net.Mail.MailMessage ToMailMessage() { // Construct an empty MailMessage to which we will gradually build up to look like the current Message object (this) System.Net.Mail.MailMessage message = new System.Net.Mail.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; // Add the first plain text version as the body, if it exists MessagePart firstPlainTextVersion = FindFirstPlainTextVersion(); if (firstPlainTextVersion != null) { message.Body = firstPlainTextVersion.GetBodyAsText(); message.BodyEncoding = firstPlainTextVersion.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 plainText version, therefore // we should skip that one if (textVersion == firstPlainTextVersion) { 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) { message.From = Headers.From.ToMailAddress(); } if (Headers.ReplyTo != null) { message.ReplyTo = Headers.ReplyTo.ToMailAddress(); } if (Headers.Sender != null) { message.Sender = Headers.Sender.ToMailAddress(); } foreach (MailAddress to in Headers.To) { if (to != null) { message.To.Add(to.ToMailAddress()); } } foreach (MailAddress cc in Headers.Cc) { if (cc != null) { message.CC.Add(cc.ToMailAddress()); } } foreach (MailAddress bcc in Headers.Bcc) { if (bcc != null) { message.Bcc.Add(bcc.ToMailAddress()); } } return(message); }
/// <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); } }