简体   繁体   中英

C# Sending SMTP using System.Threading

I have developed an application which primarily sends email messages uses SMTP. Sending messages one by one is fine, however I am looking to speed the process. I have created multiple instances of the SmtpClient as well as messages to avoid conflict among each other. Because of the separate instances, I assumed performing .Send() on multiple threads would work well. However, something with my Thread code alone is not working, because I can not send even one email on one thread using this code. I simply receive a vague "Failure sending mail" exception. I will post code that works, and the Thread that does not work. Could someone share what they believe may be the cause?

Note I am not currently looking to use the newer async capabilities but instead leveraging Thread

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 Working Thread 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
    })

    {
        Thread T1 = new Thread(delegate() { SMTP.Send(message); } );
        T1.Start();
    }

Solved:

var SMTP = new SmtpClient
        {
            Host = txtBxSenderHost.Text,
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(strSenderAddress, strSenderPassword)
        };

        Thread T1 = new Thread(delegate()
        {
            using (var message = new MailMessage(senderAdrress, toAddress)
            {
                Subject = strSubject,
                Body = strBody
            })
            {
                {
                    SMTP.Send(message);
                }
            }
        });

        T1.Start();

You can do it even easier by defining everything beforehand and the just calling the new thread.

//define everything above

Thread t1 = new Thread(delegate()
{

     SMTP.send(message);

});

t1.Start();

Thats what I did and it makes life easier with those curly braces

Why not just use the Smtp.SendAsync method ?

http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM