深入Lumisoft.NET组件与.NET API实现邮件发送功能的对(6)

text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, plainTextBody);
            multipartAlternative.BodyParts.Add(entity_text_plain);

//--- text/html -----------------------------------------
            string htmlText = body;//"<html>这是一份测试邮件,<img src=https://www.jb51.net/article/\"cid:test.jpg\">来自<font color=red><b>LumiSoft.Net</b></font></html>";
            MIME_Entity entity_text_html = new MIME_Entity();
            MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html);
            entity_text_html.Body = text_html;
            text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, htmlText);
            multipartAlternative.BodyParts.Add(entity_text_html);

//--- application/octet-stream -------------------------
            WebClient client = new WebClient();
            foreach (string attach in attachments.Keys)
            {
                try
                {
                    byte[] bytes = client.DownloadData(attach);
                    using (MemoryStream stream = new MemoryStream(bytes))
                    {
                        multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(stream, attachments[attach]));
                    }
                }
                catch (Exception ex)
                {
                    LogTextHelper.Error(ex);
                }
            }

#endregion

return msg;
        }


而构造Mime格式的操作如下所示。

复制代码 代码如下:


private Mime Create_Html_Attachment_Image(string mailTo, string mailFrom, string mailFromDisplay,
            string subject, string body, List<string> attachments, Dictionary<string, string> embedImages, string notifyEmail = "", string plaintTextTips = "",
            string replyEmail = "")
        {
            Mime m = new Mime();
            MimeEntity mainEntity = m.MainEntity;

mainEntity.From = new AddressList();
            mainEntity.From.Add(new MailboxAddress(mailFromDisplay, mailFrom));
            mainEntity.To = new AddressList();
            mainEntity.To.Add(new MailboxAddress(mailTo, mailTo));
            mainEntity.Subject = subject;
            mainEntity.ContentType = MediaType_enum.Multipart_mixed;

//设置回执通知
            if (!string.IsNullOrEmpty(notifyEmail) && ValidateUtil.IsEmail(notifyEmail))
            {
                mainEntity.DSN = notifyEmail;
            }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgxzd.html