C# 发邮件帮助类传送门(465端口除外): https://www.cnblogs.com/dennisdong/p/15953790.html

一、问题解惑,为什么465发送失败

查阅资料得知,.net 的自带组件System.Net.Mail发送邮件支持Explicit SSL但是不支持Implicit SSL,国内大部门邮件服务器都是Implicit SSL,所以无法通过465端口发邮件

有人说了,那干嘛要用呢,我用25不好好的么,为什么不用25呢?

这个问题问得好,很多云服务器像阿里、腾讯购买的新机都是把25端口封禁的,想要用25端口需要手动申请解封,据阿里工作客服所说,审核通过率极低

那么今天就记录一下如何使用465端口成功发邮件

二、解决方案

1. 可以使用CDO.Message发送邮件

如何引用CDO.Message? cod.message的引用位置: C:\Windows\System32\cdosys.dll

CDO.Message objMail = new CDO.Message();
try
{
objMail.To = "接收邮件账号";
objMail.From = "发送邮件账号";
objMail.Subject = "subject";//邮件主题string strHTML = @"";
strHTML = strHTML + "这里可以填写html内容";
objMail.HTMLBody = strHTML;//邮件内容
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;//设置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.qq.com";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "发送邮件账号登录密码";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//这一句指示是否使用ssl
objMail.Configuration.Fields.Update();
objMail.Send();
}
catch (Exception ex) { throw ex; }
finally { }
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
objMail = null;

2. 使用System.web.mail发送邮件(仅适用于Web应用程序)

System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
try
{
mail.To = "收件人邮箱";
mail.From = "发件人邮箱";
mail.Subject = "subject";
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Body = "body"; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "发件人邮箱"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "发件人邮箱密码"); //set your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//set port
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//set is ssl
System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";
System.Web.Mail.SmtpMail.Send(mail);
//return true;
}
catch (Exception ex)
{
ex.ToString();
}

3. 使用 MailKit

需要NuGet两个包 MimeKit、MailKit

using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.IO; public static void SendMailKit(string[] tos)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("发件人名称", AppConfig.From)); foreach (var s in tos)
{
if (!string.IsNullOrWhiteSpace(s))
{
message.To.Add(new MailboxAddress("收件人名称", s));
}
} message.Subject = "邮件标题"; //邮件标题
var builder = new BodyBuilder
{
//TextBody = "Hey geffzhang<br>DennisDong"//不支持Html
HtmlBody = "Hey geffzhang<br>DennisDong"//支持Html
}; //添加附件
//builder.Attachments.Add($@"{Directory.GetCurrentDirectory()}\1.png");//包含图片附件,或者正文中有图片会被当成垃圾邮件退回,所以不建议放图片内容(跟Mail类库框架无关)
builder.Attachments.Add($@"{Directory.GetCurrentDirectory()}\ConsoleApp1.exe.config");
message.Body = builder.ToMessageBody(); using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true; var mSendMail = "XXX@163.com";
var mSendPwd = "XXXXX";//163和qq都是授权码,不是邮箱密码
client.Connect("smtp.163.com", 465, true);//网易、QQ支持 25(未加密),465和587(SSL加密) client.Authenticate(mSendMail, mSendPwd); try
{
client.Send(message);//发送邮件
client.Disconnect(true);
}
catch (SmtpCommandException ex)
{
Console.WriteLine(ex.ErrorCode);
}
catch (Exception ex)
{
throw ex;
}
}
}

4. 对比

比较推荐使用第三种 MailKit,如果使用的是QQ邮箱的话,C# 自带的System.Net.Mail类库也是可以的,端口写587就可以了,由于本人不喜欢使用QQ邮箱,所以提供了以上方法

参考文章:https://www.cnblogs.com/tsql/p/9078163.html

最新文章

  1. Java-数组练习5
  2. windows 获取以及更改CMD控制台编码[转]
  3. npm 配置全局文件
  4. subilme增加对markdown的高亮支持
  5. 3DMAX 建立场景 工作流程
  6. 转:Redis作者谈Redis应用场景
  7. Servlet的学习之Session(3)
  8. jQuery中.bind() .live() .delegate() .on()的区别 和 三种方式写光棒事件 动画
  9. 018-继承-OC笔记
  10. Windows apache-flume-1.6.0+Kafka+Es
  11. asp.net core 系列 13 日志
  12. ubuntu安装后环境配置
  13. php mysql 编码问题
  14. lua --- 逻辑运算符小结
  15. PyQt5--GridLayoutMultiLine
  16. servlet的使用
  17. Android LayoutInflater深度解析
  18. hdu多校4
  19. Yii2.0 下的 load() 方法的使用
  20. cookie登录

热门文章

  1. (C++) C++ template笔记 -- template关键字及typename关键字
  2. 解决PyQt5报错defaultServiceProvider::requestService(): no service found for..
  3. 长度最小子数组-LeetCode209 滑动窗口
  4. 编程思想的转变 软件开发目录规范 collections、time、datetime、 random模块
  5. Python如何像awk一样分割字符串
  6. 一个简单的rust字符串时钟
  7. 降本超30%,智聆口语通过 TKE 注册节点实现 IDC GPU 节点降本增效实践
  8. python数据分析与可视化【思维导图】
  9. ArcGIS工具 - 批量删除空图层
  10. 【原创】linux实时应用如何printf输出不影响实时性?