阿里云服务器 qq smtp发送邮件失败
本地测试可以发送,放服务器上面就发送邮件失败
报错信息:
2021-04-22 18:06:07,509 [3] ERROR Common.Log4netHelper - System.Net.Mail.SmtpException: 发送邮件失败。 ---> System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 183.3.225.42:25
在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
在 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
在 System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
在 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
在 System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
在 System.Net.Mail.SmtpClient.GetConnection()
在 System.Net.Mail.SmtpClient.Send(MailMessage message)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Net.Mail.SmtpClient.Send(MailMessage message)
在 Common.SendMessage.SendEmailCode(String mailTo, String code) 位置 D:\新建文件夹\用户管理\UserManager\Common\SendMessage.cs:行号 132
折腾了半天,各种端口都开放了都不行,最后发现好像是阿里云默认屏蔽25端口,需要开启587端口发送
发送邮件代码
try
{
string mailSubject = "奇迹之耀";
string mailContent = "您的验证码为" + code;
SmtpClient mailClient = new SmtpClient("smtp.qq.com");
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Port = 587;
//Credentials登陆SMTP服务器的身份验证.
mailClient.Credentials = new NetworkCredential("1731071838@qq.com", "glfnvkbqeqimdfgh");//邮箱,
MailMessage message = new MailMessage(new MailAddress("1731071838@qq.com"), new MailAddress(mailTo));//发件人,收件人
message.IsBodyHtml = true;
// message.Bcc.Add(new MailAddress("tst@qq.com")); //可以添加多个收件人
message.Body = mailContent;//邮件内容
message.Subject = mailSubject;//邮件主题
//Attachment 附件
//Attachment att = new Attachment(@"C:/hello.txt");
//message.Attachments.Add(att);//添加附件
//Console.WriteLine("Start Send Mail....");
//发送....
mailClient.Send(message); // 发送邮件
return "ok";
}
catch (Exception e)
{
Log4netHelper.Error(e);
return e.Message;
}