欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能

 更新時(shí)間:2020年06月16日 22:09:39   作者:愛(ài)奔跑的碼農(nóng)  
這篇文章主要介紹了ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

具體代碼如下所示:

# 導(dǎo)包
  首先我們需要導(dǎo)入 MailKit NuGet包,NuGet安裝包命令在下方拓展介紹中。
# 引用命名空間
using MailKit.Net.Smtp;
using MimeKit;
# 郵件發(fā)送幫助類(lèi)
 /// <summary>
 /// 發(fā)送郵件
 /// </summary>
 /// <param name="Name">發(fā)件人名字</param>
 /// <param name="receive">接收郵箱</param>
 /// <param name="sender">發(fā)送郵箱</param>
 /// <param name="password">郵箱密碼</param>
 /// <param name="host">郵箱主機(jī)</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代表一封電子郵件的對(duì)象
  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 OAuth2 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;
 }

 借助這一個(gè)簡(jiǎn)單的郵件發(fā)送類(lèi)我們就可以已經(jīng)可以實(shí)現(xiàn)郵件發(fā)送功能了。

# 拓展(NuGet常用命令) 

1、安裝指定版本:install-package <程序包名> -version <版本號(hào)>

2、更新包:Update-Package <程序包名>

3、重新安裝所有Nuget包(整個(gè)解決方案都會(huì)重新安裝)

  update-package -reinstall

4、重新安裝指定項(xiàng)目所有Nuget包

  update-package -project <項(xiàng)目名稱(chēng)> -reinstall

5、正常卸載:uninstall-package <程序包名>

6、強(qiáng)制卸載:Uninstall-Package <程序包名> -Force

總結(jié)

以上所述是小編給大家介紹的ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論