SmtpException - The operation has timed out

2019-05-07 15:39发布

问题:

Here is my code:

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;

using (client as IDisposable)
{
    foreach (MailAddress addr in Addresses)
    {
        if (addr != null) 
        {
            try 
            {
                message.To.Clear();
                message.To.Add(addr);
                client.Send(message);
            }
            catch (Exception ex) 
            {
                Log(ex);
            }
            i++;
        }
    }
}

every 100 seconds, I log a message saying

The operation has timed out.

Is this a client side setting or on the actual mail server?

回答1:

The problem happens when you are not able to connect to SMTP server and thats why this timeout message occur. So this message occurs on your client when your client is not able to connect to your SMTP Server:

100 second is default value as described below: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.timeout.aspx

There could be several issue why this prblem could occur i.e. Wrong SMTP address, SMTP reject, Port setting, SSL configuration etc which you need to fix.



回答2:

I was also expeiencing this timeout. The problem seemed to be that the email I was trying to send out had a single email address but repeated 30+ times (this was a dev environment where the real recipient email addresses were change to a dev one). Cutting this number (to about 20) resolved the issue. Obviously, this is an issue specific to my own SMTP server, but the recipient list is something to look at if all else fails.



标签: c# smtp