我们知道目前 .NET Core 还不支持 SMTP 协议,当我么在使用到发送邮件功能的时候,需要借助于一些第三方组件来达到目的,今天给大家介绍两款开源的邮件发送组件,它们分别是 MailKit 和 FluentEmail , 下面我对它们分别进行介绍。
在 ASP.NET Core 中,可以使用 MailKit 来发送邮件,它支持跨平台,并且支持 IMAP, POP3, SMTP 等协议。
你可以使用下面的方式安装:
1 2 |
<span class="hljs-keyword">Install</span>-<span class="hljs-keyword">Package</span> MailKit |
下面是一个简单的发送邮件的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span class="dt"><span class="hljs-keyword">var</span></span> message = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">MimeMessage</span> (); message.<span class="fu">From</span>.<span class="fu">Add</span> (<span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">MailboxAddress</span> (<span class="st"><span class="hljs-string">"Joey Tribbiani"</span></span>, <span class="st"><span class="hljs-string">"joey@friends.com"</span></span>)); message.<span class="fu">To</span>.<span class="fu">Add</span> (<span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">MailboxAddress</span> (<span class="st"><span class="hljs-string">"Mrs. Chanandler Bong"</span></span>, <span class="st"><span class="hljs-string">"chandler@friends.com"</span></span>)); message.<span class="fu">Subject</span> = <span class="st"><span class="hljs-string">"星期天去哪里玩?"</span></span>; message.<span class="fu">Body</span> = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">TextPart</span> (<span class="st"><span class="hljs-string">"plain"</span></span>) { Text = <span class="st"><span class="hljs-string">"我想去故宫玩,如何"</span></span> }; <span class="kw"><span class="hljs-keyword">using</span></span> (<span class="dt"><span class="hljs-keyword">var</span></span> client = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">SmtpClient</span> ()) { <span class="co"><span class="hljs-comment">// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)</span></span> client.<span class="fu">ServerCertificateValidationCallback</span> = (s,c,h,e) => <span class="kw"><span class="hljs-keyword">true</span></span>; client.<span class="fu">Connect</span> (<span class="st"><span class="hljs-string">"smtp.friends.com"</span></span>, <span class="dv"><span class="hljs-number">587</span></span>, <span class="kw"><span class="hljs-keyword">false</span></span>); <span class="co"><span class="hljs-comment">// Note: since we don't have an OAuth2 token, disable</span></span> <span class="co"><span class="hljs-comment">// the XOAUTH2 authentication mechanism.</span></span> client.<span class="fu">AuthenticationMechanisms</span>.<span class="fu">Remove</span> (<span class="st"><span class="hljs-string">"XOAUTH2"</span></span>); <span class="co"><span class="hljs-comment">// Note: only needed if the SMTP server requires authentication</span></span> client.<span class="fu">Authenticate</span> (<span class="st"><span class="hljs-string">"joey"</span></span>, <span class="st"><span class="hljs-string">"password"</span></span>); client.<span class="fu">Send</span> (message); client.<span class="fu">Disconnect</span> (<span class="kw"><span class="hljs-keyword">true</span></span>); } |
如果你要发送的 Body 内容是 HTML 的话,你可以使用下面这种:
1 2 3 4 |
<span class="dt"><span class="hljs-keyword">var</span></span> bodyBuilder = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">BodyBuilder</span>(); bodyBuilder.<span class="fu">HtmlBody</span> = <span class="hljs-string">@</span><span class="st"><span class="hljs-string">"<b>This is bold and this is <i>italic</i></b>"</span></span>; message.<span class="fu">Body</span> = bodyBuilder.<span class="fu">ToMessageBody</span>(); |
Fluent Email 这个也是一个开源项目,利用它,你可以使用 Razor 模板来发送邮件,并且可以集成一些第三方的邮件发送程序比如 Mailgun等,但是此包只在 .NET 4.6 下才支持 SMTP 。你可以使用如下命令来安装它:
1 2 |
<span class="hljs-keyword">Install</span>-<span class="hljs-keyword">Package</span> FluentEmail.Razor |
你可以使用最基本的方式来发送邮件,很简单如下:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="co"><span class="hljs-comment">//注意: .NET 4.6 才支持</span></span> Email.<span class="fu">DefaultSender</span> = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">SmtpSender</span>(); <span class="dt"><span class="hljs-keyword">var</span></span> email = Email .<span class="fu">From</span>(<span class="st"><span class="hljs-string">"foo@email.com"</span></span>) .<span class="fu">To</span>(<span class="st"><span class="hljs-string">"bar@email.com"</span></span>, <span class="st"><span class="hljs-string">"bob"</span></span>) .<span class="fu">Subject</span>(<span class="st"><span class="hljs-string">"星期天去哪里玩?"</span></span>) .<span class="fu">Body</span>(<span class="st"><span class="hljs-string">"我想去故宫玩,如何?"</span></span>); <span class="hljs-keyword">await</span> email.<span class="fu">SendAsync</span>(); |
或者,你可以使用 Razor 模板来发送:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="co"><span class="hljs-comment">//注意: .NET 4.6 才支持</span></span> Email.<span class="fu">DefaultSender</span> = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">SmtpSender</span>(); <span class="co"><span class="hljs-comment">// Using Razor templating package</span></span> Email.<span class="fu">DefaultRenderer</span> = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">RazorRenderer</span>(); <span class="hljs-keyword">var</span> template = <span class="st"><span class="hljs-string">"Dear @Model.Name, You are totally @Model.Compliment."</span></span>; <span class="dt"><span class="hljs-keyword">var</span></span> email = Email .<span class="fu">From</span>(<span class="st"><span class="hljs-string">"bob@hotmail.com"</span></span>) .<span class="fu">To</span>(<span class="st"><span class="hljs-string">"somedude@gmail.com"</span></span>) .<span class="fu">Subject</span>(<span class="st"><span class="hljs-string">"woo nuget"</span></span>) .<span class="fu">UsingTemplate</span>(template, <span class="kw"><span class="hljs-keyword">new</span></span> { Name = <span class="st"><span class="hljs-string">"Luke"</span></span>, Compliment = <span class="st"><span class="hljs-string">"Awesome"</span></span> }); |
Email.DefaultRenderer
是告诉FulentEmail 使用哪个渲染器(你也可以自己实现一个自己的),然后提供了一个 template 模板,内容为 Razor 语法的模板字符串,然后使用 UsingTemplate
来进行渲染呈现。
加入你的邮件 Razor 模板文件比较大,用字符串来表示的话不太优雅,那么你可以把模板文件放到磁盘上,然后使用如下方式来加载:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="co"><span class="hljs-comment">//注意: .NET 4.6 才支持</span></span> Email.<span class="fu">DefaultSender</span> = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">SmtpSender</span>(); Email.<span class="fu">DefaultRenderer</span> = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">RazorRenderer</span>(); <span class="dt"><span class="hljs-keyword">var</span></span> email = Email .<span class="fu">From</span>(<span class="st"><span class="hljs-string">"foo@email.com"</span></span>) .<span class="fu">To</span>(<span class="st"><span class="hljs-string">"bar@email.com"</span></span>, <span class="st"><span class="hljs-string">"bob"</span></span>) .<span class="fu">Subject</span>(<span class="st"><span class="hljs-string">"星期天去哪里玩?"</span></span>) .<span class="fu">UsingTemplateFromFile</span>($<span class="st"><span class="hljs-string">"{Directory.GetCurrentDirectory}/EmailTemplage.cshtml"</span></span>, <span class="kw"><span class="hljs-keyword">new</span></span> {Name =<span class="st"><span class="hljs-string">"Luke"</span></span>}) |
可能有一些人对 Mailgun 还不太清楚,Mailgun 是国外的一个邮件服务公司,比如著名的 Github 的邮件服务就托管在它的上面,免费的 Maingun 账户每个月可以发送 10000 封邮件,对于很多中小网站足够用了。
当使用 Mailgun 来发送邮件的时候,你首先需要去注册一个账号,然后可以利用 Mailgun 提供的 Rest API 来管理发送或者接收的邮件。使用 FluentEmail 集成的 Mailgun只需要添加如下包:
1 2 |
<span class="hljs-keyword">Install</span>-<span class="hljs-keyword">Package</span> FluentEmail.Mailgun |
注册完 Mailgun 之后会给你分配一个 API Key 和一个二级域名,在程序种,你需要如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="co"><span class="hljs-comment">// 同时支持 .NET Core 和 .NET Framework</span></span> <span class="dt"><span class="hljs-keyword">var</span></span> sender = <span class="kw"><span class="hljs-keyword">new</span></span> <span class="fu">MailgunSender</span>( <span class="st"><span class="hljs-string">"sandboxcf5f41bbf2f84f15a386c60e253b5fe8.mailgun.org"</span></span>, <span class="co"><span class="hljs-comment">// Mailgun 二级域名</span></span> <span class="st"><span class="hljs-string">"key-8d32c046d7f14ada8d5ba8253e3e30df"</span></span> <span class="co"><span class="hljs-comment">// Mailgun API Key</span></span> ); Email.<span class="fu">DefaultSender</span> = sender; <span class="dt"><span class="hljs-keyword">var</span></span> email = Email .<span class="fu">From</span>(<span class="st"><span class="hljs-string">"foo@email.com"</span></span>) .<span class="fu">To</span>(<span class="st"><span class="hljs-string">"bar@email.com"</span></span>, <span class="st"><span class="hljs-string">"bob"</span></span>) .<span class="fu">Subject</span>(<span class="st"><span class="hljs-string">"星期天去哪里玩?"</span></span>) .<span class="fu">Body</span>(<span class="st"><span class="hljs-string">"我想去故宫玩,如何?"</span></span>); <span class="hljs-keyword">await</span> email.<span class="fu">SendAsync</span>(); |
通过上述示例我们可以看出 MailKit 和 FluentEmail 各有优缺点。MailKit 的优点是支持的协议比较多,而且是跨平台的,但是缺点是没有提供对 Razor的支持,并且如果使用 Mailgun 的话需要自己进行集成。 那么 FlentEmail 的优点是提供了 Razor 模板的支持并且封装了 Mailgun, 缺点是 SMTP 协议还没有提供对 .NET Core 的支持。
综上所述,如果你使用 Mailgun 来发送邮件的话,那么 FluentEmail 是你应该选择的,如果你想使用 SMTP 协议链接到自己的邮件服务器来发送邮件的话,那么你应该用 MailKit。
本文地址:http://www.cnblogs.com/savorboard/p/aspnetcore-email.html
作者博客:Savorboard
欢迎转载,请在明显位置给出出处及链接