0 votes
by (340 points)

We want to send out reminder emails via autheticated SMTP in small batches, fewer than one hundred (100) per batch. The content of each email is different, and each has only one recipient.

The process is running as a Windows Service that fetches the necessary info from the database every few minutes, and replaces placeholders in the email html with data from the database. There is no GUI to worry about blocking.

Should we loop through the data records as in this pseudocode, sending one email at a time?

  instantiate smtpClient
  authenticate smtpClient
  for each datum in data
      message = BuildEmailMessage(datum)
      Task.Run(()=> smtpClient.Send(message))    
  next
  smtpClient.Disconnect()
Applies to: Rebex Secure Mail

1 Answer

0 votes
by (58.9k points)

Actually the SMTP client is not capable of sending multiple messages at the same time, so I would recommend to connect and login to the SMTP server once and then send the messages one by one as in this code:

        Task.Run(() =>
        {
            Smtp smtp = new Smtp();
            smtp.Connect("server", SslMode);
            smtp.Login("user", "password");

            for (int i = 0; i < 100; i++)
            {
                var message = BuildEmailMessage(...);
                smtp.Send(message);

                Console.WriteLine("sent email {0}.", i);
            }
            smtp.Disconnect();
        });
...