我是靠谱客的博主 鲜艳抽屉,最近开发中收集的这篇文章主要介绍MailKit发送邮件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

smtp发送邮件偶尔会遇到超时的情况,故换MailKit发送尝试。 

引用MailKit包时,注意版本需要支持当前Framework的版本


                using (var smtp = new MailKit.Net.Smtp.SmtpClient())
                {

                    MimeMessage mail = new MimeMessage();
                    //发送方
                    mail.From.Add(new MailboxAddress(request.From, request.From));
                    //接受方
                    mail.To.Add(new MailboxAddress(request.To, request.To));
                    //标题
                    mail.Subject = request.Subject;
                    //抄送邮箱
                    if (request.CC != null && request.CC.Length > 0)
                    {
                        foreach (var c in request.CC)
                        {
                            mail.Cc.Add(new MailboxAddress(c));
                        }
                    }
                    //密件抄送邮箱
                    if (request.BCC != null && request.BCC.Length > 0)
                    {
                        foreach (var c in request.BCC)
                        {
                            mail.Bcc.Add(new MailboxAddress(c));
                        }
                    }

                    var multipart = new Multipart("mixed");
                    //内容
                    if (!string.IsNullOrEmpty(request.HtmlBody))
                    {
                        var Html = new TextPart(MimeKit.Text.TextFormat.Html)
                        {
                            Text = request.HtmlBody
                        };
                        multipart.Add(Html);
                    }
                    //附件
                    if (request.Attachments != null)
                    {
                        foreach (var attachment in request.Attachments)
                        {
                            if ((!string.IsNullOrEmpty(attachment.MediaType) && attachment.MediaType == MediaTypeNames.Application.Zip) || attachment.FileName.EndsWith(".zip"))
                            {
                                var attimg = new MimePart(MediaTypeNames.Application.Zip)
                                {
                                    ContentObject = new ContentObject(new MemoryStream(attachment.FileData), ContentEncoding.Default),
                                    ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                                    ContentTransferEncoding = ContentEncoding.Base64,
                                    FileName = attachment.FileName,
                                    IsAttachment = true,
                                };
                                multipart.Add(attimg);
                            }
                            else if ((!string.IsNullOrEmpty(attachment.MediaType) && attachment.MediaType == MediaTypeNames.Application.Pdf) || attachment.FileName.EndsWith(".pdf"))
                            {
                                var attimg = new MimePart(MediaTypeNames.Application.Pdf)
                                {
                                    ContentObject = new ContentObject(new MemoryStream(attachment.FileData), ContentEncoding.Default),
                                    ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                                    ContentTransferEncoding = ContentEncoding.Base64,
                                    FileName = attachment.FileName,
                                    IsAttachment = true,
                                };
                                multipart.Add(attimg);
                            }
                            else
                            {
                                var attimg = new MimePart()
                                {
                                    ContentObject = new ContentObject(new MemoryStream(attachment.FileData), ContentEncoding.Default),
                                    ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                                    ContentTransferEncoding = ContentEncoding.Base64,
                                    FileName = attachment.FileName,
                                    IsAttachment = true,
                                };
                                multipart.Add(attimg);
                            }
                        }
                    }
                    mail.Body = multipart;

                    smtp.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    //连接邮箱服务器
                    smtp.Connect(request.SmtpServer ?? smtpServer, request.Port, request.EnableSsl);
                    //登录认证
                    if (!string.IsNullOrWhiteSpace(request.FromUserId))
                    {
                        smtp.Authenticate(request.FromUserId, request.PasswordOfFrom); //一些邮件服务器除发送人邮箱外需要发送人Id验证
                    }
                    else
                    {
                        smtp.Authenticate(request.From, request.PasswordOfFrom);
                    }

                    smtp.Timeout = 600000;
                    Yahee.Logging.Log4net.LoggerManager.ErrorLog.Error("SendEmailService begin: " + request.Subject);
                    smtp.Send(mail);
                    smtp.Disconnect(true);
                }

    /// <summary>
    /// 邮寄发送接口参数类
    /// </summary>
    public class SendEmailRequest
    {
        /// <summary>
        /// 发送人邮箱
        /// </summary>
        public string From
        {
            get;
            set;
        }

        /// <summary>
        /// 发送人Id(一些邮件服务器除发送人邮箱外需要发送人Id验证)
        /// </summary>
        public string FromUserId
        {
            get;
            set;
        }
        
        /// <summary>
        /// 密码明文
        /// 空值。使用admin@yahee.com.cn发送邮件,
        /// 非空值,使用发送人邮箱发送邮件
        /// </summary>
        public string PasswordOfFrom
        {
            get;
            set;
        }

        /// <summary>
        /// 收件邮箱
        /// </summary>
        public string To
        {
            get;
            set;
        }

        /// <summary>
        /// 抄送邮箱集合
        /// </summary>
        public string[] CC
        {
            get;
            set;
        }

        /// <summary>
        /// 密送邮箱集合
        /// </summary>
        public string[] BCC
        {
            get;
            set;
        }
        /// <summary>
        /// 主题
        /// </summary>
        public string Subject
        {
            get;
            set;
        }
        /// <summary>
        /// 带html的内容
        /// </summary>
        public string HtmlBody
        {
            get;
            set;
        }

        /// <summary>
        /// 需要附加的附件集合
        /// </summary>
        public FileAttachment[] Attachments
        {
            get;
            set;
        }

        /// <summary>
        /// 是否新增发送记录
        /// </summary>
        private bool _isInsertSendEmail = true;
        public bool IsInsertSendEmail
        {
            set
            {
                _isInsertSendEmail = value;
            }
            get
            {
                return _isInsertSendEmail;
            }
        }

        /// <summary>
        /// 邮件服务器
        /// //必须和密码同步
        /// </summary>
        public string SmtpServer
        {
            get;
            set;
        }

        /// <summary>
        /// 发送端口
        /// </summary>
        public int Port
        {
            get;
            set;
        }

        /// <summary>
        /// 是否需要SSL验证
        /// </summary>
        public bool EnableSsl
        {
            get;
            set;
        }

        /// <summary>
        /// 是否需要身份验证
        /// </summary>
        public bool IsSendAuth
        {
            get;
            set;
        }
    }


    public class FileAttachment
    {
        /// <summary>
        /// 附件在邮件中的名称
        /// </summary>
        public string FileName
        {
            get;
            set;
        }

        /// <summary>
        /// 文件内容
        /// </summary>
        public byte[] FileData
        {
            get;
            set;
        }

        /// <summary>
        /// 附件类型
        /// </summary>
        /// <see cref="System.Net.Mime.MediaTypeNames"/>
        public string MediaType
        {
            get;
            set;
        }
    }
附件添加

            string fileAttachmentPath2 = @"E:softwareXXX.zip";
            FileAttachment attachment2 = new FileAttachment();
            attachment2.FileName = "XXX.zip";
            attachment2.FileData = System.IO.File.ReadAllBytes(fileAttachmentPath2);
            attachment2.MediaType = System.Net.Mime.MediaTypeNames.Application.Zip;
            allAttachments.Add(attachment2);

最后

以上就是鲜艳抽屉为你收集整理的MailKit发送邮件的全部内容,希望文章能够帮你解决MailKit发送邮件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(29)

评论列表共有 0 条评论

立即
投稿
返回
顶部