I have a problem, I'm trying send a mail using one structure like this one:
private void SendMail()
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("mymail@mail.com", "mypassword"),
EnableSsl = true
};
client.Send("mymail@mail.com", "AdressClient@mail.com", "Subject", "Body");
}
Well, this structure works just fine, but cause a lag in my main program, for that reason I woudl like to execute the SendMail function in another Thread, like this:
private void ButtonTestSendMail_Click(object sender, EventArgs e)
{
//THREAD
Thread ThreadSendEmail = new Thread(new ThreadStart(this.SendMail));
ThreadSendEmail .IsBackground = true;
ThreadSendEmail .Start();
}
private void SendMail()
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("mymail@mail.com", "mypassword"),
EnableSsl = true
};
client.Send("mymail@mail.com", "AdressClient@mail.com", "Subject", "Body");
}
Well at this point I notice that I need to change the parameters of the mail, like, AdressClient@mail.com, Suject and Body.
But I don't know How :/
How Can I write a thread that accepte parameters, something like this:
private void ButtonTestSendMail_Click(object sender, EventArgs e)
{
string Body = "BodyTest";
string AdressMail = "AdressMail@test.com";
string Subject = "SubjectTest";
//THREAD
Thread ThreadSendEmail = new Thread(new ThreadStart(this.SendMail(Body,AdressMail,Subject));
ThreadSendEmail .IsBackground = true;
ThreadSendEmail .Start();
}
private void SendMail(string Body, string Adress, string Subject)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("mymail@mail.com", "mypassword"),
EnableSsl = true
};
client.Send("mymail@mail.com", Adress, Subject, Body);
}
I tried but doesnt work, I have a MethodName Expected and a notification that the ERROR is in this line:
Thread ThreadSendEmail = new Thread(new ThreadStart(this.SendMail(Body,AdressMail,Subject));
How Can I write a thread that accepte parameters? Thank you for help! Best Regards!