zoukankan      html  css  js  c++  java
  • AspNetCore 目前不支持SMTP协议(基于开源组件开发邮件发送,它们分别是MailKit 和 FluentEmail )

    net所有的功能都要重新来一遍,集成众多类库,core任重道远,且发展且努力!!

    我们都知道,很多的邮件发送都是基于这个SMTP协议,但现在的.net core对这方面还不太支持,所以我们选择这两个组件MailKit 和 FluentEmail

    MailKit与fluentEmail

    在 ASP.NET Core 中,可以使用 MailKit 来发送邮件,它支持跨平台,并且支持 IMAP, POP3, SMTP 等协议。

    你可以使用下面的方式安装:

    Install-Package MailKit

    直接show代码吧!!
    using MimeKit;
    using System;
    using System.IO;
    
    namespace SMTP协议
    {
        class Program
        {
            static void Main(string[] args)
            {
                TestSendMailDemo();
            }
            public static void TestSendMailDemo()
            {
                var message = new MimeKit.MimeMessage();
                message.From.Add(new MimeKit.MailboxAddress("zara", "947099752@qq.com"));
                message.To.Add(new MimeKit.MailboxAddress("zaranet", "zaranet@163.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 = @"C:UsersMACHENIKEDesktopa.png";
                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.qq.com", 465, true);
    
                    // 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 = "947099752@qq.com";
                    var mailPassword = "xxxx";
                    client.Authenticate(mailFromAccount, mailPassword);
                    client.Send(message);
                    client.Disconnect(true);
                }
                fs.Dispose();
            }
        }
    }

    结果:

    BUG::  

    1.其中通过mailkit发送的时候  发送方必须要打开自己的POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 一般是在自己的个人中心   2018-11-12  16:11:41

    2.一般来说授权码是在个人中心,而且一定有帮助 里面有关于他的服务什么的

     FluentEmail 

      

    private static void TestSmtpClient()
            {
                MailMessage mymail = new MailMessage();
                mymail.From = new System.Net.Mail.MailAddress(mailFrom);
                mymail.To.Add(mailTo);
                mymail.Subject = string.Format("C#自动发送邮件测试 From geffzhang TO {0}",mailTo);
                mymail.Body = @"<p>Hey geffzhang<br><p>不好意思,我在测试程序,刚才把QQ号写错了,Sorry!<br><p>-- Geffzhang<br>";
                mymail.IsBodyHtml = true;
                mymail.Attachments.Add(new Attachment(path));
    
                System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
                smtpclient.Port = 587;
                smtpclient.UseDefaultCredentials = false;
                smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpclient.Host = "smtp.live.com";
                smtpclient.EnableSsl = true;
                smtpclient.Credentials = new System.Net.NetworkCredential(mailFromAccount, mailPassword);
                try
                {
                    smtpclient.Send(mymail);
                    Console.WriteLine("发送成功");
    
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine("发送邮件失败.请检查是否为qq邮箱,并且没有被防护软件拦截" + ex);
    
                }
            }
        }
    

      

  • 相关阅读:
    Leetcode665.Non-decreasing Array非递减数组
    在MyEclipse中把多行代码用一快捷键注释掉
    struts2中addFieldError()方法
    [bzoj2588][Spoj10628]Count on a tree_主席树
    [bzoj3123][Sdoi2013]森林_主席树_启发式合并
    [bzoj1500][NOI2005]维修数列_非旋转Treap
    [bzoj1452][JSOI2009]Count_树状数组
    [bzoj1369][Baltic2003]Gem_树形dp_结论题
    [bzoj1195][HNOI2006]最短母串_动态规划_状压dp
    [bzoj2242][Sdoi2011]计算器_exgcd_BSGS
  • 原文地址:https://www.cnblogs.com/ZaraNet/p/9946200.html
Copyright © 2011-2022 走看看