Esempio n. 1
0
        /// <summary>
        /// 转换成.Net 自带的MailMessage
        /// </summary>
        /// <returns></returns>
        public MailMessage ToMailMessage()
        {
            MailMessage message = new MailMessage();

            message.Subject         = this.Headers.Subject;
            message.SubjectEncoding = Encoding.UTF8;
            MessagePart preferredVersion = this.FindFirstHtmlVersion();

            if (preferredVersion != null)
            {
                message.IsBodyHtml = true;
            }
            else
            {
                preferredVersion = this.FindFirstPlainTextVersion();
            }
            if (preferredVersion != null)
            {
                message.Body         = preferredVersion.GetBodyAsText();
                message.BodyEncoding = preferredVersion.BodyEncoding;
            }
            IEnumerable <MessagePart> textVersions = this.FindAllTextVersions();

            foreach (MessagePart textVersion in textVersions)
            {
                if (textVersion != preferredVersion)
                {
                    MemoryStream  stream      = new MemoryStream(textVersion.Body);
                    AlternateView alternative = new AlternateView(stream);
                    alternative.ContentId   = textVersion.ContentId;
                    alternative.ContentType = textVersion.ContentType;
                    message.AlternateViews.Add(alternative);
                }
            }
            IEnumerable <MessagePart> attachments = this.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 (this.Headers.From != null && this.Headers.From.HasValidMailAddress)
            {
                message.From = this.Headers.From.MailAddress;
            }
            if (this.Headers.ReplyTo != null && this.Headers.ReplyTo.HasValidMailAddress)
            {
                message.ReplyToList.Add(this.Headers.ReplyTo.MailAddress);
            }
            if (this.Headers.Sender != null && this.Headers.Sender.HasValidMailAddress)
            {
                message.Sender = this.Headers.Sender.MailAddress;
            }
            foreach (RfcMailAddress to in this.Headers.To)
            {
                if (to.HasValidMailAddress)
                {
                    message.To.Add(to.MailAddress);
                }
            }
            foreach (RfcMailAddress cc in this.Headers.Cc)
            {
                if (cc.HasValidMailAddress)
                {
                    message.CC.Add(cc.MailAddress);
                }
            }
            foreach (RfcMailAddress bcc in this.Headers.Bcc)
            {
                if (bcc.HasValidMailAddress)
                {
                    message.Bcc.Add(bcc.MailAddress);
                }
            }
            return(message);
        }
Esempio n. 2
0
 protected abstract T CaseLeaf(MessagePart messagePart);