I developed an application that basically sends emails using SMTP. Sending messages one at a time is OK, however I'm looking to speed up the process. I created several instances SmtpClient, as well as messages, to avoid a conflict between myself. Due to individual instances, I assumed that executing .Send()for multiple threads would work well. However, something with my code Threaddoes not work, because I can not send at least one email address in one stream using this code. I just get an undefined exception "Failure sending mail". I will write code that works, and Threadthat does not work. Can someone share what they think might be the reason?
Note I'm not currently looking for new features async, but instead usedThread
Working declaration and method call:
var SMTP = new SmtpClient
{
Host = txtBxSenderHost.Text,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(strSenderAddress, strSenderPassword)
};
using (var message = new MailMessage(senderAdrress, toAddress)
{
Subject = strSubject,
Body = strBody
})
{
SMTP.Send(message);
}
NOT declaring a work topic and calling a method:
var SMTP = new SmtpClient
{
Host = txtBxSenderHost.Text,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(strSenderAddress, strSenderPassword)
};
using (var message = new MailMessage(senderAdrress, toAddress)
{
Subject = strSubject,
Body = strBody
})
{
Thread T1 = new Thread(delegate() { SMTP.Send(message); } );
T1.Start();
}
source
share