c#使用MailKit发送邮件

using System;
using System.IO;
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;
namespace commom
{
    public class EmailHelper
    {
        //Nuget包管理添加Mailkit
        /// 
        /// 服务邮箱
        /// 
        private string _STEPNAME = "smtp.qq.com";
        /// 
        /// 服务邮箱端口
        /// 
        private int _STEPPORT = 587;
        /// 
        /// 发送方邮箱
        /// 
        private string _USEREMAIL;
        /// 
        /// 发送方邮箱Smtp授权码
        /// 
        private string _PASSWORD;
        /// 
        /// 发送方邮箱归属人,昵称
        /// 
        private string _EMAILBLONGER;
        private string email;
        private string emailBlonger;
        private string smtp;

        /// 
        /// 邮箱配置
        /// 
        /// 
        /// 
        public EmailHelper(string email, string smtp)
        {
            this._USEREMAIL = email;
            this._PASSWORD = smtp;
        }

        public EmailHelper(string email, string emailBlonger, string smtp)
        {
            this._USEREMAIL = email;
            this._PASSWORD = smtp;
            this._EMAILBLONGER = emailBlonger;
        }
        /// 
        /// 邮箱发送类
        /// 
        /// 发送方邮箱
        /// 发送方名称
        /// 邮件标题
        /// 发送的文字内容
        /// 发送的html内容
        /// 发送的附件,找不到的就自动过滤
        /// 
        public string SendEmail(string toEmaill, string toEmailBlonger, string subject, string text, string html, string path)
        {
            try
            {
                MimeMessage message = new MimeMessage();
                //发送方
                message.From.Add(new MailboxAddress(this._EMAILBLONGER, this._USEREMAIL));
                //接受方
                message.To.Add(new MailboxAddress(toEmailBlonger, toEmaill));
                //标题
                message.Subject = subject;
                //创建附件
                var multipart = new Multipart("mixed");
                //文字内容
                if (!string.IsNullOrEmpty(text))
                {
                    var plain = new TextPart(TextFormat.Plain)
                    {
                        Text = text
                    };
                    multipart.Add(plain);
                }
                //html内容
                if (!string.IsNullOrEmpty(html))
                {
                    var Html = new TextPart(TextFormat.Html)
                    {
                        Text = html
                    };
                    multipart.Add(Html);
                }
                if (!string.IsNullOrEmpty(path))
                {
                    var pathList = path.Split(';');
                    foreach (var p in pathList)
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(p.Trim()))
                            {
                                var attimg = new MimePart()
                                {//"image", "png"方法里带参数的话
                                    ContentObject = new ContentObject(File.OpenRead(p), ContentEncoding.Default),
                                    ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                                    ContentTransferEncoding = ContentEncoding.Base64,
                                    FileName = Path.GetFileName(path)
                                };
                                multipart.Add(attimg);
                            }
                        }
                        catch (FileNotFoundException ex)
                        {
                            //找不到文件就不提交附件了
                        }
                    }
                }
                //赋值邮件内容
                message.Body = multipart;
                //开始发送
                using (var client = new SmtpClient())
                {
                    client.Connect(this._STEPNAME, this._STEPPORT, false);
                    client.Authenticate(this._USEREMAIL, this._PASSWORD);
                    client.Send(message);
                    client.Disconnect(true);
                }
                return "邮件发送成功";
            }
            catch (Exception)
            {
                return "邮箱发送失败";
            }
        }
        /// 
        /// 邮箱发送类,不用输入用户昵称的
        /// 
        /// 发送方邮箱
        /// 发送方名称
        /// 邮件标题
        /// 发送的文字内容
        /// 发送的html内容
        /// 发送的附件,多附件用;隔开
        /// 
        public void SendEmail(string toEmaill, string subject, string text, string html, string path)
        {
            try
            {
                MimeMessage message = new MimeMessage();
                //发送方
                message.From.Add(new MailboxAddress(this._USEREMAIL));
                //接受方
                message.To.Add(new MailboxAddress(toEmaill));
                //标题
                message.Subject = subject;
                //创建附件
                var multipart = new Multipart("mixed");
                //文字内容
                if (!string.IsNullOrEmpty(text))
                {
                    var plain = new TextPart(TextFormat.Plain)
                    {
                        Text = text
                    };
                    multipart.Add(plain);
                }
                //html内容
                if (!string.IsNullOrEmpty(html))
                {
                    var Html = new TextPart(TextFormat.Html)
                    {
                        Text = html
                    };
                    multipart.Add(Html);
                }
                if (!string.IsNullOrEmpty(path))
                {//修改为多附件,
                    var pathList = path.Split(';');
                    foreach (var p in pathList)
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(p.Trim()))
                            {
                                var attimg = new MimePart()
                                {//"image", "png"方法里带参数的话
                                    ContentObject = new ContentObject(File.OpenRead(p), ContentEncoding.Default),
                                    ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                                    ContentTransferEncoding = ContentEncoding.Base64,
                                    FileName = Path.GetFileName(path)
                                };
                                multipart.Add(attimg);
                            }
                        }
                        catch (FileNotFoundException ex)
                        {
                            //找不到文件就不提交附件了
                        }
                    }
                }
                //赋值邮件内容
                message.Body = multipart;
                //开始发送
                using (var client = new SmtpClient())
                {
                    client.Connect(this._STEPNAME, this._STEPPORT, false);
                    client.Authenticate(this._USEREMAIL, this._PASSWORD);
                    client.Send(message);
                    client.Disconnect(true);
                }
               // return "邮件发送成功";
            }
            catch (Exception ex)
            {
               // return "邮箱发送失败";
            }
        }
    }

    /// 
    /// 测试调用方法
    /// 
    public class Test{
        private email=new EmailHelper("发送的邮箱","Smtp授权码");
        public void SendEmail()
        {
            email.SendEmail("对方的邮箱", "对方邮箱名称也可以是邮箱","标题", "内容", "这是html标记的文本", "附加地址")
        }
    }
}

 

你可能感兴趣的:(c#)