启用TLS1.2后,Asp.net无法发送邮件

时间:2018-07-23 06:02:36

标签: c# asp.net web gmail

我有Windows专用服务器,托管着20多个使用asp.net构建的网站。 我所有的网站都使用Gmail SMTP完美地发送了电子邮件,但是2天前,我要求技术支持启用TLS1.2和禁用TLS1.0;当发生这种情况时,所有电子邮件都停止了!

我已经尝试了所有解决方案Here,但仍然没有用! 最后,我尝试使用其他电子邮件提供商而不是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="myemail@gmail.com">
    <network defaultCredentials="false" host="smtp.gmail.com" password="Pass here" userName="myemail@gmail.com" enableSsl="true" port="587" />
  </smtp>

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决方案; 经过大量搜索后,我发现必须在global.asax.cs文件的Application_Start()函数中添加此行代码。

 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;