TransBind
asked on
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);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
monarch_ilhan:good point, but i am using different accounts
should be
t.Start(new string[]{address, user, pass, server});
since its strings ;)