net core调用MimeKit发送QQ邮件

一.在QQ邮箱内申请授权码,具体参考请官方文档

二.具体代码

public void TestSendMailDemo()
        {
            MimeMessage message = new MimeMessage();
            //发件人
            message.From.Add(new MailboxAddress("张三丰", "1072****2@qq.com"));
            //收件人
            message.To.Add(new MailboxAddress("测试", "3170***95@qq.com"));
            //标题
            message.Subject = "测试标题内容";
            //产生一个支持HTml 的TextPart
            TextPart body = new TextPart(TextFormat.Html)
            {
                Text = "<h1>测试内容</h1>"
            };

            //创建Multipart添加附件
            Multipart multipart = new Multipart("mixed");
            multipart.Add(body);
            //附件
            //string absolutePath = @"F:\桌面\新建文件夹\mysql1.sql";
            //MimePart attachment = new MimePart()
            //{
            //    //读取文件,只能用绝对路径
            //    ContentObject = new ContentObject(File.OpenRead(absolutePath), ContentEncoding.Default),
            //    ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
            //    ContentTransferEncoding = ContentEncoding.Base64,
            //    //文件名字
            //    FileName = Path.GetFileName(absolutePath)
            //};
            //multipart.Add(attachment);

            //正文内容,发送
            message.Body = multipart;
            //message.Body = body;
            using (SmtpClient client = new SmtpClient())
            {
                //Smtp服务器
                client.Connect("smtp.qq.com", 587, false);
                //登录,发送
                //特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
                client.Authenticate("10****62@qq.com", "授权码");

                client.Send(message);
                //断开
                client.Disconnect(true);
                Console.WriteLine("发送邮件成功");
            }
}

参考文献:http://www.mamicode.com/info-detail-1839328.html

 

上一篇:MailKit系列之附件分离


下一篇:multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded