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
orUseDefaultCredentials = 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.