zoukankan      html  css  js  c++  java
  • 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();
    }

  • 相关阅读:
    Web_0002:关于MongoDB的操作
    JN_0008:win下通过cmd进入指定目录
    H5_0008:链接分享图片和判断平台
    H5_0007:使用base64做为背景图片
    JN_0007:微信昵称设置小数字
    H5_0006:JS判断PC,平板,手机平台的方法
    H5_0002:微信分享设置
    Web_0001:关于阿里云防盗链Referer,CDN加速,OSS自定义域名的操作
    H5_0001:localStorage本地存储
    JN_0006:MongoDB未授权访问漏洞处理
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/6722752.html
Copyright © 2011-2022 走看看