0

I have windows dedicated server hosting more than 20 websites all built using asp.net. All my sites were sending emails using Gmail SMTP perfectly, but 2 days ago I asked tech-support to enable TLS1.2 and disable TLS1.0; when this happened all the emails stopped!

I have tried all solutions Here but still no use! Finally, I tried to use another email provider instead of Gmail and the emails are sent successfully.

Does anybody know what happens to Gmail services with TLS1.2?

Here is code sample:

 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;
    }

And this is web.config data:

<smtp from="myemail@gmail.com">
    <network defaultCredentials="false" host="smtp.gmail.com" password="Pass here" userName="myemail@gmail.com" enableSsl="true" port="587" />
  </smtp>
Mohamed Hasan
  • 207
  • 4
  • 13
  • 1
    It would be awesome if you could provide a [mcve]. – mjwills Jul 23 '18 at 06:04
  • What is the version of .net you have used in your web app? – Hamlet Hakobyan Jul 23 '18 at 06:07
  • see: [C# ASP.NET Send Email via TLS](https://stackoverflow.com/questions/2057227/c-sharp-asp-net-send-email-via-tls) try enable SSL – lex.xu Jul 23 '18 at 06:08
  • are you sending mails from code?or directly from GMAIL itself?if you are sending mail from code then you have to add a mail which is registered / authenticated mail from your hosted site. – Ramlal S Jul 23 '18 at 06:09
  • @RamlalS I already added the email in the web.config file: – Mohamed Hasan Jul 23 '18 at 06:12
  • @lex.xu; I am adding the SSL n the web.config file; and it was working perfectly before enabling this feature. I tried to remove it but I get the error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required" – Mohamed Hasan Jul 23 '18 at 06:18
  • i think this problem exits with only bigrock site because i face the same problem when i'm working on php to send mails. Then i registered a mail in my hosting then i added that mail to my code. Then it works fine for me.check it once.Thank you. – Ramlal S Jul 23 '18 at 06:19
  • @HamletHakobyan; I am using .Net Framework 4.5 – Mohamed Hasan Jul 23 '18 at 06:21
  • Possible duplicate of [Sending email in .NET through Gmail](https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – VDWWD Jul 23 '18 at 07:32

1 Answers1

3

Finally, I have found a solution; after a lot of search, I found that I had to add this line of code in the Application_Start() function in global.asax.cs file.

 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
Mohamed Hasan
  • 207
  • 4
  • 13
  • I found it on this site: https://codeshare.co.uk/blog/how-to-force-a-net-website-to-use-tls-12/ – Mohamed Hasan Jul 23 '18 at 06:36
  • FWIW this solution also works for scenarios where you need to force instances of `System.Net.WebClient` to use TLS 1.2. Of course, ideal solution is to target a newer framework but sometimes you need to get a legacy project targeting a legacy framework to work in a pinch! – Mike Wright Oct 01 '20 at 18:29