0

I have quite the conundrum. I'm trying to send an email with a simple Console application using the following snippet of code (obviously removed actual emails used):

private static void sendEmail()
{
   var client = new SmtpClient("smtp.gmail.com", 587)
   {
       Credentials = new NetworkCredential("sender@gmail.com", "myPassword"),
       EnableSsl = true
   };
    
   client.Send("sender@gmail.com", "receiver@gmail.com", "Email Subject", "Some body message");        
}

Then I get the Server does not support secure connections error:

Unhandled exception. System.Net.Mail.SmtpException: Server does not support secure connections.
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

Now the reason it's a real pickle is because I have another Console application that I wrote back in July 2021 that uses the EXACT SAME method (literally a copy-paste of my sendEmail() method) and it works with no issues.

My initial instinct when something of the sort happens (works in one environment but not the other) is to clear my cache. Done that for my Visual Studio, but unfortunately still the same outcome

Despite knowing that my code works in one environment, I still went ahead and tried various combinations of some StackOverflow suggestions such as:

  • using different ports (25, 465)
  • turn off/on Ssl (EnableSsl = false)
  • explicitly specify UseDefaultCredentials = true or UseDefaultCredentials = false
  • explicitly specify DeliveryMethod = SmtpDeliveryMethods.Network

My gmail account has the "Less secure apps" turned on and I followed the Gmail help for POP/IMAP settings. The Gmail account in question does not use two-factor authentication. I also followed the various suggestions in what seems to be the latest active thread on stackOverflow regarding this issue over here but nothing worked.

This is driving me somewhat bunkers considering the same settings work with no issues in another Console app.

Any help would be greatly appreciated.

  • You might want to make an [Application Password](https://support.google.com/accounts/answer/185833?hl=en) rather than making your account less secure (and using your full Google password). Though apparently you tried that already. – gunr2171 Oct 28 '21 at 18:17
  • You could try to enable tracing as described [here](https://stackoverflow.com/a/27421247/10318835) on both environments and compare the logs, maybe you get some hints. – Steeeve Oct 30 '21 at 12:59

0 Answers0