Sending 2000 emails [closed]

耗尽温柔 提交于 2019-12-04 21:10:03

You should really be kinder to your mail server but SMTP client can handle more than one email and you'd get better use of your servers hardware if you did this asynchronously like so ...

async Task SendMail(ICollection<MailMessage> messages)
{
    using (var client = new SmtpClient())
    {
        foreach (MailMessags m in messages)
        {
            try
            {
                await client.SendAsync(m);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

I suspect the reason for the exception being thrown at the moment is due to repeated connection attempts, so the server maybe "acting out" as a means to say "stop poking me with repeated connections".

The code sample above will determine how many connections to make on your behalf and communicate more "politely" with the server, also as you have pointed out, the documentation points at a few other scenarios in which this approach is better suited to.

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