ASP.NET Core中使用MialKit实现邮件发送功能
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了ASP.NET Core中使用MialKit实现邮件发送功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
具体代码如下所示:
# 导包 首先我们需要导入 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, aept 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
总结
以上所述是长沙网络推广给大家介绍的ASP.NET Core中使用MialKit实现邮件发送功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
如果你觉得本文对你有帮助,欢迎网络推广网站推广转载,烦请注明出处,谢谢!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程