您好,登錄后才能下訂單哦!
小編給大家分享一下ASP.NET Core中怎么使用MialKit實現(xiàn)郵件發(fā)送功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體代碼如下所示:
# 導(dǎo)包 首先我們需要導(dǎo)入 MailKit NuGet包,NuGet安裝包命令在下方拓展介紹中。 # 引用命名空間 using MailKit.Net.Smtp; using MimeKit; # 郵件發(fā)送幫助類 /// <summary> /// 發(fā)送郵件 /// </summary> /// <param name="Name">發(fā)件人名字</param> /// <param name="receive">接收郵箱</param> /// <param name="sender">發(fā)送郵箱</param> /// <param name="password">郵箱密碼</param> /// <param name="host">郵箱主機</param> /// <param name="port">郵箱端口</param> /// <param name="subject">郵件主題</param> /// <param name="body">郵件內(nèi)容</param> /// <returns></returns> public async Task<bool> SendMailAsync(string Name, string receive, string sender, string password, string host, int port, string subject, string body) { try { # MimeMessage代表一封電子郵件的對象 var message = new MimeMessage(); # 添加發(fā)件人地址 Name 發(fā)件人名字 sender 發(fā)件人郵箱 message.From.Add(new MailboxAddress(Name, sender)); # 添加收件人地址 message.To.Add(new MailboxAddress("", receive)); # 設(shè)置郵件主題信息 message.Subject = subject; # 設(shè)置郵件內(nèi)容 var bodyBuilder = new BodyBuilder() { HtmlBody = body }; message.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; // Note: since we don't have an OAuth3 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); client.CheckCertificateRevocation = false; //client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12; client.Connect(host, port, MailKit.Security.SecureSocketOptions.Auto); // Note: only needed if the SMTP server requires authentication client.Authenticate(sender, password); await client.SendAsync(message); client.Disconnect(true); return true; } } catch (Exception ex) { } return false; }
借助這一個簡單的郵件發(fā)送類我們就可以已經(jīng)可以實現(xiàn)郵件發(fā)送功能了。
# 拓展(NuGet常用命令)
1、安裝指定版本:install-package <程序包名> -version <版本號>
2、更新包:Update-Package <程序包名>
3、重新安裝所有Nuget包(整個解決方案都會重新安裝)
update-package -reinstall
4、重新安裝指定項目所有Nuget包
update-package -project <項目名稱> -reinstall
5、正常卸載:uninstall-package <程序包名>
6、強制卸載:Uninstall-Package <程序包名> -Force
以上是“ASP.NET Core中怎么使用MialKit實現(xiàn)郵件發(fā)送功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。