• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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

0
TransBind
Asked:
TransBind
  • 2
2 Solutions
 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now