Threads when sending emails

I have a simple function that sends emails, how can I use streams to speed up email delivery?

Sample code would be perfect.

+4
source share
8 answers

Use SendAsync isntead.

+8
source

Check out the link below to demonstrate the sendAsync method. [MSDN]

http://msdn.microsoft.com/en-ca/library/x5x13z6h(VS.80).aspx

+3
source

You can run this function in another thread. As a SendMail sender function, you can:

ThreadPool.QueueUserWorkItem(delegate { SendMail(message); }); 
+2
source

IN 4.0 you can use the following,

 new Thread(x => SendMail(message)).Start(); 

and

 public static void SendEmail(MailMessage message) { using (SmtpClient client = new SmtpClient("smtp.XXXXXX.com")) { client.Send(message); } } 
+2
source

Create your class with the static void method, which will make your class start doing what you want to do in a separate thread, with something like:

 using System; using System.Threading; class Test { static void Main() { Thread newThread = new Thread(new ThreadStart(Work.DoWork)); newThread.Start(); } } class Work { Work() {} public static void DoWork() {} } 

Another alternative is to use the ThreadPool class if you do not want to manage your threads yourself.

Additional information on topics - http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx

Additional information about ThreadPool - http://msdn.microsoft.com/en-us/library/3dasc8as(VS.80).aspx

+1
source

If a separate stream does not speed up email delivery, however. All he will do is return control back to the calling method faster. Therefore, if you do not need to do this, I would not even bother with this.

+1
source

When you send emails using multiple streams, be careful that your ISP is identified as spam. It is better to choose smaller batches with some delay between each batch.

+1
source

You know that it would be better and easier to create the back of the application and send emails every 30 minutes. Drop the information into the database that needs to be sent back and forth, create an application pool that runs every 30 minutes. When it starts, you can send an email. No need to wait for your event handler to send an email ...

It works for us. Just thought it would help you.

-1
source

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


All Articles