SmtpClient.Send randomly causes SmtpException 4.7.0 timeout waiting for client input

有些话、适合烂在心里 提交于 2019-12-05 14:36:28
CodeRedick

This definitely sounds like a server or network issue. Reference this serverfault post for instance: https://serverfault.com/questions/344684/exchange-2007-email-error-451-4-7-0-timeout-waiting-for-client-input

It could be a firewall problem if there is a firewall between your machine and the exchange server. A firewall blocks ports between ip addresses, it doesn't matter where the email is going at all. Firewalls are the bane of my programming existence, so definitely ask your net admin if there is a firewall between the servers and have him check if it is working.

It could also be something like a bad NIC (network card) on the server or even a bad cable. Yes, your network admin said the network is rock solid... but never trust network admins. ;)

It could also be a problem with YOUR network card, network settings or ethernet cable, actually.

Have you tried running this from different computers?

You can fire if off async like by changing your code as follows. You will need to check the AggregateException to see if something bad happened on any of the executing threads.

static void Main(string[] args)
{
    for (int i = 0; i < 100; i++)
    {
        string html = "<h1>TEST</h1>";
        Task.Factory.StartNew(() =>
        {
            using (MailMessage mail = new MailMessage("sender@domain.com", "receiver@domain.com"))
            {
                mail.Subject = "Test";
                mail.IsBodyHtml = true;
                mail.Body = html;

                using (SmtpClient client = new SmtpClient("<internal ip address>"))
                {
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential("<user name>", "<password>");

                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Send(mail);
                }
            }
        }).ContinueWith(aex => {
            if (aex.Exception != null)
            {
                //add some logic to deal with the exception
            }
        });
    }
}

it is better to define SmtpClient before the loop, same problem happened with me and this solved it.

using System;
using System.Net.Mail;

namespace SendMail
{
    class Program
    {
        SmtpClient client = new SmtpClient("<internal ip address>");

        static void Main(string[] args)
        {

            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("<user name>", "<password>");
            //client.EnableSsl = true;
            //client.Port = 25;    
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            for (int i = 0; i < 100; i++)
            {
                string html = "<h1>TEST</h1>";

                using (MailMessage mail = new MailMessage("sender@domain.com", "receiver@domain.com"))
                {
                    mail.Subject = "Test";
                    mail.IsBodyHtml = true;
                    mail.Body = html;

                    client.Send(mail);
                }
            }
        }
    }
}

I'm fairly new to this messaging issue. I have currently been working on a messaging application and occasionally this exception is thrown into the system. So far the answers I have picked up on the internet point out that this is an error in the email sender's authentication. Perhaps the limitation of daily submissions and the hourly send limit may inhibit SMTP server authentication of the email. Example, in case smtps.bol.com.br has a limit of 50 emails per hour, having to have a 60 minute interval to send a new batch of messages. I have done my tests here and I do not have a conclusive answer, but I believe that maybe the way is this. If they find the definitive solution, do not forget to post.

I was getting similar error couple of years back on Amazon Aws server. It seems my security company "AllCovered.com" had to do a exception to my server ip address to allow send smtp emails.

I do get this error if anytime a new server created is forgotten to be put in exception.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!