• Netcore使用MailKit进行邮件发送


    public void TestSendMailDemo()
    {
    var message = new MimeKit.MimeMessage();
    message.From.Add(new MimeKit.MailboxAddress("hotmail", "china-psu@hotmail.com"));
    message.To.Add(new MimeKit.MailboxAddress("qq", "283775652@qq.com"));
    message.Subject = "This is a Test Mail";
    var plain = new MimeKit.TextPart("plain")
    {
    Text = @"不好意思,我在测试程序,Sorry!"
    };
    var html = new MimeKit.TextPart("html")
    {
    Text = @"<p>Hey geffzhang<br>
    <p>不好意思,我在测试程序,Sorry!<br>
    <p>-- Geffzhang<br>"
    };
    // create an image attachment for the file located at path
    var path = "D:\雄安.jpg";
    var fs = File.OpenRead(path);
    var attachment = new MimeKit.MimePart("image", "jpeg")
    {

    ContentObject = new MimeKit.ContentObject(fs, MimeKit.ContentEncoding.Default),
    ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
    ContentTransferEncoding = MimeKit.ContentEncoding.Base64,
    FileName = Path.GetFileName(path)
    };
    var alternative = new MimeKit.Multipart("alternative");
    alternative.Add(plain);
    alternative.Add(html);
    // now create the multipart/mixed container to hold the message text and the
    // image attachment
    var multipart = new MimeKit.Multipart("mixed");
    multipart.Add(alternative);
    multipart.Add(attachment);
    message.Body = multipart;
    using (var client = new MailKit.Net.Smtp.SmtpClient())
    {
    client.Connect("smtp.live.com", 587, false);

    // Note: since we don't have an OAuth2 token, disable
    // the XOAUTH2 authentication mechanism.
    client.AuthenticationMechanisms.Remove("XOAUTH2");

    // Note: only needed if the SMTP server requires authentication
    var mailFromAccount = "china-psu@hotmail.com";
    var mailPassword = "xxxxxxxxxxxxxxxxxxx";
    client.Authenticate(mailFromAccount, mailPassword);

    client.Send(message);
    client.Disconnect(true);
    }
    fs.Dispose();
    }

  • 相关阅读:
    cv2 Qt Platform plugin "cocoa" not found error
    开发scrapy web界面(一)
    java2smali python 粘合脚本
    react如何设置代理
    Nginx启动不了失败原因
    前端,后端,服务器如何部署,转载
    匿名函数普通函数和构造函数
    闭包的认识
    各种命名规范,打好基础才能建设高楼
    mongoose常用操作
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/6722752.html
Copyright © 2020-2023  润新知