0

A connection attempt failed because the connected party did not appropriately respond after some time, or an established connection failed because the connected host has failed to respond 142.251.5.109:587

I have tried many solutions (SendGrid, MailTrap, SmtpClient, MailKit...) on google, allowed ports on the firewall, and used different email ids... but I am getting the same error every time. Now I suspect that there might be some issue with my system or my IP which is blocked everywhere for sending email via code. Any Idea? The last code which I used

private async Task SendEmail(IdentityMessage message)
{
    try
    {
        var msg = new MimeKit.MimeMessage();
        msg.From.Add(new MimeKit.MailboxAddress("name", "email"));
        msg.To.Add(new MimeKit.MailboxAddress("Hello!", message.Destination));//email
        msg.Subject = message.Subject;
        msg.Body = new MimeKit.TextPart(MimeKit.Text.TextFormat.Html) { Text = message.Body.ToString() };
        using (var client = new MailKit.Net.Smtp.SmtpClient())
        {                    
            client.Connect("smtp.gmail.com", 587, false);//also tried different smtp, different ports 25, 465, 2525 and 3rd parameter to True etc                 
            client.Authenticate("email", "password");
            client.Send(msg);
            client.Disconnect(true);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
Gokul G.K
  • 110
  • 1
  • 8
  • Never do throw ex where ex is a caught exception. You'll lose the stack trace. Just do throw; instead. Or in this case, removal the useless try/catch entirely. – mason Aug 07 '22 at 05:15
  • Gmail isn't great to send emails from. It's generally better to use a service like SendGrid that's meant for programmatically sending emails. – mason Aug 07 '22 at 05:16
  • With most free SMTP email service there are some limitations sending mass email. I can imagine you have tried so many times that either you are blocked or limited to send more emails. Imo use mail API some offer up to 1000 to 10000 email a month for free and than little fee for more emails – Maytham Fahmi Aug 07 '22 at 05:19
  • Thanks for the help, Actually it might be caused by many tires (not more than 100) but Now it has been resolved by allowing port 587 inbound and outbound rules over Firewall. – Precious Friend Aug 07 '22 at 05:30

0 Answers0