SSL发送邮件提示:无法从传输连接中读取数据: net_io_connectionclosed。

使用System.Net.Mail发送邮件时,发现使用25端口发送邮件正常,但使用465+SSL端口发送失败(163、qq、阿里云本身都不行,qq的587端口可用,但我们必须用vip.163发送),提示:无法从传输连接中读取数据: net_io_connectionclosed。

但使用阿里云的服务器25端口禁封,必须使用465发送。发现使用以下的代码可以正常发送,记录一下

// 邮件主题
System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
//邮件主题
mmsg.Subject = "主题";
mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
//邮件正文
mmsg.Body = "内容";
//正文编码
mmsg.BodyEncoding = Encoding.UTF8;
//优先级
mmsg.Priority = System.Web.Mail.MailPriority.Normal;
//发件者邮箱地址
mmsg.From = "[email protected]";
//收件人收箱地址
mmsg.To = "[email protected]";

mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]");
//密码(授权码)
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxx");
//端口
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);
//是否ssl
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
//Smtp服务器
System.Web.Mail.SmtpMail.SmtpServer = "smtp.vip.163.com";
try
{
SmtpMail.Send(mmsg);
Response.Write("发送成功");
}
catch (Exception ex)
{
Response.Write("发送失败,失败原因:" + ex.Message);
}

猜你喜欢

转载自www.cnblogs.com/xuzhimin/p/9132223.html