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:\Users\MACHENIKE\Desktop\a.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();
}
}
}

结果:

AspNetCore 目前不支持SMTP协议(基于开源组件开发邮件发送,它们分别是MailKit 和 FluentEmail )

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); }
}
}

  

上一篇:SDWebImage实现原理详解


下一篇:PHP格式化数字和SMARTY格式化数字的方法