C# 发送邮件问题
阿里云服务25端口不开放解决方案
要用System.Web.Mail,之所以不行,是因为System.Net.Mail,中SmtpClient只支持Explicit SSL但是不支持Implicit SSL,而扩展SSl的SMTP会话是起于未加密的通道即加密的25端口,然后使用SSL切换到安全通信465,如果你的服务器禁用25,扩展ssl开始都没办法开始。所以发不出去,所以用SmtpClient行不通,而已改用System.Web.Mail。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mail; namespace Demo { /// <summary> /// 要用System.Web.Mail,之所以不行,是因为System.Net.Mail,中SmtpClient只支持Explicit SSL但是不支持Implicit SSL,而扩展SSl的SMTP会话是起于未加密的通道即加密的25端口,然后使用SSL切换到安全通信465,如果你的服务器禁用25,扩展ssl开始都没办法开始。所以发不出去,所以用SmtpClient行不通,而已改用System.Web.Mail。 /// MailToolSSL mail = new MailToolSSL("smtp.163.com", "password", "", "*****@163.com"); /// mail.CreateMail("1596782257@qq.com", "", "", "测试主题", "内容--人生得意须尽欢", "HTML", true); /// mail.SendMail(); /// </summary> public class MailToolSSL { public MailMessage _MailMessage; public int _SenderPort = 465; //端口 public string _SenderServerHost; //smtp.163.com public string _SenderPassword; //密码 public string _SenderUsername; // public string _SenderMail; //*****@163.com public bool _EnableSsl = true; //SSL public MailToolSSL(string SenderServerHost, string SenderPassword, string SenderUsername, string SenderMail) { this._SenderServerHost = SenderServerHost; this._SenderPassword = SenderPassword; this._SenderUsername = SenderUsername; this._SenderMail = SenderMail; } /// <summary> /// 创建Mail /// </summary> /// <param name="recipient">收件人</param> /// <param name="cc">CC</param> /// <param name="bcc">BCC</param> /// <param name="subject">标题</param> /// <param name="content">内容</param> /// <param name="format">格式</param> /// <param name="sslEnable"></param> public ReturnResults CreateMail(string recipient, string cc, string bcc, string subject, string content, string format, bool sslEnable) { try { _MailMessage = new MailMessage(); _MailMessage.To=recipient; _MailMessage.From = _SenderMail; if (!string.IsNullOrEmpty(cc)) _MailMessage.Cc = cc; if (!string.IsNullOrEmpty(bcc)) _MailMessage.Bcc = bcc; _MailMessage.Subject = subject; _MailMessage.Body = content; if (format.ToUpper().Equals("HTML")) _MailMessage.BodyFormat = System.Web.Mail.MailFormat.Html; else _MailMessage.BodyFormat = System.Web.Mail.MailFormat.Text; _MailMessage.BodyEncoding = System.Text.Encoding.UTF8; _MailMessage.Priority = MailPriority.Normal; _EnableSsl = sslEnable; return ReturnResults.Success("Mail创建成功"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return ReturnResults.Error("Create Mail 异常" + ex.ToString()); } } public ReturnResults SendMail() { try { if (_MailMessage != null) { _MailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证 _MailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _MailMessage.From); //邮箱登录账号 _MailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _SenderPassword); //登录密码 _MailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口 _MailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密 System.Web.Mail.SmtpMail.SmtpServer = _SenderServerHost; //smtp.163.com System.Web.Mail.SmtpMail.Send(_MailMessage); return ReturnResults.Success("Mail发送成功"); } return ReturnResults.Error("创建的Mail为空 ,检查CreateMail 方法"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return ReturnResults.Error("Mail发送异常" + ex.ToString()); } } public class ReturnResults { public bool state { get; set; } public string msg { get; set; } private ReturnResults(bool _state, string _msg) { this.state = _state; this.msg = _msg; } public static ReturnResults Success(String msg) { return new ReturnResults(true, msg); } public static ReturnResults Error(String msg) { return new ReturnResults(false, msg); } public static ReturnResults Anything(bool state, String msg) { return new ReturnResults(state, msg); } } } }
本文作者:浅绿色i
本文链接:https://www.cnblogs.com/xfweb/p/15709774.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用