Send email to various accounts using Threads

I would like to send email to various accounts using Threads. A thread should be created for an individual email account. Is this a correct way to do this? Any errors or should I change anything? I have about 20 email accounts and I don't want to create 20 different functions for every account. How can I create one function and execute it as 20 different threads. Basically path to that function email network credentials and start it as a new thread.
using System;
using System.Threading;
 
namespace CSharp
{
   public class ExampleThreads
   {
      public static void Main()
      {
         ExampleThreads sample = new ExampleThreads();
         sample.CreateThreads();
      }
 
      public void CreateThreads()
      {
         Thread firstThread = new Thread( new ThreadStart( SendMail_1 ) );
         Thread secondThread = new Thread( new ThreadStart( SendMail_2 ) );
 
         firstThread.Start(); //account one
         secontThread.Start(); //account two
      }
 
      private void SendMail_1()
      {
	  MailMessage message = new MailMessage(txtFrom.Text, txtTo1.Text,   txtSubject.Text, txtBody.Text);
	  SmtpClient emailClient = new SmtpClient();
	  emailClient.Credentials = new System.Net.NetworkCredential("user1", "pwd1");
	  emailClient.Host = "mail.youdomain1.com
	  emailClient.Port = 25;
	  emailClient.Send(message);
      }
 
      private void SendMail_2()
      {
  	  MailMessage message = new MailMessage(txtFrom.Text, txtTo2.Text,   txtSubject.Text, txtBody.Text);
	  SmtpClient emailClient = new SmtpClient();
	  emailClient.Credentials = new System.Net.NetworkCredential("user2", "pwd2");
	  emailClient.Host = "mail.youdomain2.com
	  emailClient.Port = 25;
	  emailClient.Send(message);
      }
   }
}

Open in new window

LVL 7
TransBindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Solar_FlareCommented:
the best way would be to have a method that accepts an email address etc and creates a new thread, that way you can just call this method 20 times with your email addresses.

you will need to pass the parameters to the thread. I have put some example code below to show the idea
public static void Main()
      {
         ThreadedSendMail(//set of params for account);
         ThreadedSendMail(//other account info);
      }
 
      public void ThreadedSendMail(string address, string user, string pass, string server)
      {
        System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(SendMail));
        t.Start(new int[]{address, user, pass, server});
       
      }
 
      private void SendMail(object args)
      {
         
//send email using arguments - args[0] contains email address, args[1] contains username and so on
      }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Solar_FlareCommented:
t.Start(new int[]{address, user, pass, server});

should be

t.Start(new string[]{address, user, pass, server});


since its strings ;)
0
monarch_ilhanCommented:
Even you send mails in separate threads, since smtp works synchronously , it wont effect the performance as much as you desired. I mean, unless smtpclient does not complete sending first message, it wont accept second for sending. A delay will occur.
0
TransBindAuthor Commented:
monarch_ilhan:good point, but i am using different accounts
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.