C # Sending SMTP using System.Threading

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();
    }
+5
source share
3 answers

It is decided:

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();
+11
source

You can make it even easier by pre-defining everything and simply calling a new thread.

//define everything above

Thread t1 = new Thread(delegate()
{

     SMTP.send(message);

});

t1.Start();

Here's what I did, and it makes life easier with these curly braces.

+2
source

Why not just use the Smtp.SendAsync method?

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

+1
source

All Articles