Sending 2000 letters

Is it possible to instantiate an SMTP client once and send 2000 letters to it synchronously?

We get the following error when creating an instance of the SMTP client for each message sent, using the code also inserted at the end of this message:

System.Net.Mail.SmtpException: service unavailable, closing the transmission channel. Server response: 4.4.1 Connection time from

I suspect that the SMTP client instance for each message causes this, but not sure. The code is as follows.

List<MailMessage> messages = GetMailMessages();
foreach( MailMessags m in messages)
{
    try
    {
        SmtpClient client = new SmtpClient();//SHOULD THIS BE PLACED OUTSIDE AND BEFORE THE LOOP
        client.Send(m);
    }
    catch (Exception)
    {
        throw;
    }
}

EDIT 1:

MSDN. (http://msdn.microsoft.com/en-us/library/ee706942%28v=vs.110%29.aspx). , , SMTP- MailMessages.

, SmtpClient SMTP-      , SMTP-.
     , SMTP     . TLS .           SMTP- .      , ,     .

+2
1

, SMTP- , , , ...

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

, , "" " ".

, "" , , , .

+1

Source: https://habr.com/ru/post/1527796/


All Articles