I've been stuck with this issue for a long time now. My application can send email without problem from my local and another server but there's this one server where it always fails with this error:
System.IO.IOException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Sample Code:
public static void SendEmail(EmailCreateViewModel viewModel)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender Name", "test@senderemail.com"));
message.To.Add(new MailboxAddress("Recipient Name", "test@recipientemail.com"));
message.Subject = viewModel.Subject;
message.Body = new TextPart("plain") { Text = viewModel.TextBody };
using (var client = new SmtpClient(new ProtocolLogger("C:\\smtp.log")))
{
try
{
if(viewModel.BypassServerCertificateValidationCallback)
client.ServerCertificateValidationCallback = (sender, certificate, certChainType, errors) => true;
client.SslProtocols = System.Security.Authentication.SslProtocols.Tls | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12;
client.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);
client.Authenticate(viewModel.User, viewModel.Password);
client.Send(message);
client.Disconnect(true);
}
catch (Exception ep)
{
Log("failed to send email with the following error:");
Log(ep.ToString());
}
}
}
smtp.log:
Connected to smtp://smtp.office365.com:587/?starttls=always
S: 220 SI2PR02CA0002.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 16 Jun 2022 03:02:14 +0000
C: EHLO <Insert Server Name>
S: 250-SI2PR02CA0002.outlook.office365.com Hello [<Insert Server IP>]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO <Insert Server Name>
S: 250-SI2PR02CA0002.outlook.office365.com Hello [<Insert Server IP>]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH LOGIN XOAUTH2
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: AUTH LOGIN
It feels like I've tried everything and don't know what else to do and the exception itself doesn't say much. I've also tried turning off the firewall and windows defender but it still fails.
To isolate the server, I've been able to send an email from the problematic server via PowerShell with similar settings (same server, port and TLS).
I hope someone can shed some light here or at least give some fresh perspective on this issue. Thanks!
In the end, we weren't able to fix the code itself. After multiple trial and errors, we tried sending the email from a .NET Core console program using the exact code. To our surprise, it worked.
What we ended up doing is convert the old .NET Framework 4.5.2 Service to the latest .NET 6 Worker Service. Tested it again in the problematic server and everything is working fine.
Our hunch is for some reason, the old libraries of .NET Framework was deemed unsecure by the SMTP server (or along the way to the SMTP server) and rejects the connection.