阿里云服务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); } } } }