# 导包

  首先我们需要导入 MailKit NuGet包,NuGet安装包命令在下方拓展介绍中。

# 引用命名空间

using MailKit.Net.Smtp;
using MimeKit;

# 邮件发送帮助类

        /// <summary>
/// 发送邮件
/// </summary>
/// <param name="Name">发件人名字</param>
/// <param name="receive">接收邮箱</param>
/// <param name="sender">发送邮箱</param>
/// <param name="password">邮箱密码</param>
/// <param name="host">邮箱主机</param>
/// <param name="port">邮箱端口</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</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();
          # 添加发件人地址 Name 发件人名字 sender 发件人邮箱
message.From.Add(new MailboxAddress(Name, sender));
          # 添加收件人地址
message.To.Add(new MailboxAddress("", receive));
          # 设置邮件主题信息
message.Subject = subject;
          # 设置邮件内容
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;
}

借助这一个简单的邮件发送类我们就可以已经可以实现邮件发送功能了。

# 拓展(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

# 参考博文

https://blog.csdn.net/sD7O95O/article/details/89334103

https://www.cnblogs.com/qulianqing/p/7413640.html

https://www.cnblogs.com/savorboard/p/aspnetcore-email.html

https://www.cnblogs.com/daizhipeng/p/10955773.html

最新文章

  1. JavaScript小例子:复选框全选
  2. 一个CMS案例实战讲解PHP代码审计入门
  3. pwd命令(转)
  4. 对DotNet分布式应用搭建的考虑
  5. MySQL删除重复记录只保留一条
  6. Entity Framework 并发处理
  7. 光盘卡在MacBook里退不出来咋办?
  8. 浅谈SQL优化入门:1、SQL查询语句的执行顺序
  9. Java的流
  10. java总结:double取两位小数的多种方法
  11. MySQL数据查询之单表查询
  12. Javascript学习之:JSON
  13. [翻译]EntityFramework Core 2.2 发布
  14. set @sql=N&#39;q&#39;,这里的N是什么意思,加与不加有什么区别
  15. wimform/对话框/Textbox基本方法
  16. java中的元数据
  17. redis教程(The little redis book中文版)
  18. 5.8 Components — Composing Components(组合组件)
  19. Linux系统格式化磁盘+挂载分区
  20. 【Python】求素数-稍加优化

热门文章

  1. ajax的jQuery的表单序列化获取参数serialize()
  2. 10.Redis 主从架构
  3. c# list集合简单快捷取随机
  4. 4、Work-Queue
  5. python批量创建txt文件,以demo.txt内的内容为文件名
  6. dp的一些练习
  7. springboot配置spring security 静态资源不能访问
  8. Maven的Scope区别笔记
  9. 【转】HTTPS 如何保证数据传输的安全性?
  10. 7.18 NOIP模拟测试5 星际旅行+砍树+超级树