我有 Windows 专用服务器,托管着 20 多个网站,全部使用 ASP.NET 构建。 我的所有网站都完美地使用 Gmail SMTP 发送电子邮件,但 2 天前我要求技术支持启用 TLS1.2 并禁用 TLS1.0;当这种情况发生时,所有电子邮件都停止了!
我已经尝试了所有解决方案这里但仍然没有用! 最后,我尝试使用其他电子邮件提供商代替 Gmail,电子邮件发送成功。
有人知道使用 TLS1.2 的 Gmail 服务会发生什么情况吗?
这是代码示例:
public static bool SendMail(MailAddressCollection TO_List, string Subject, string Body, bool Is_Html_Body, System.Collections.Hashtable Attachments)
{
MailMessage msg = new MailMessage();
msg.Subject = Subject;
msg.Body = Body;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = Is_Html_Body;
msg.Priority = MailPriority.High;
SmtpClient sc = new SmtpClient();
if (Attachments != null)
{
foreach (Attachment AttachmentObj in Attachments.Values)
{
msg.Attachments.Add(AttachmentObj);
}
}
foreach (MailAddress To_Address in TO_List)
{
msg.To.Add(To_Address);
}
sc.Send(msg);
return true;
}
这是 web.config 数据:
<smtp from="[email protected]">
<network defaultCredentials="false" host="smtp.gmail.com" password="Pass here" userName="myema[email protected]" enableSsl="true" port="587" />
</smtp>
终于,我找到了解决办法; 经过大量搜索,我发现我必须在 global.asax.cs 文件的 Application_Start() 函数中添加这行代码。
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
这对我来说就像一个魅力,非常感谢穆罕默德哈桑