我有一个问题,我正在尝试使用这样的一种结构发送邮件:
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");
}
好吧,这个结构工作得很好,但是会导致我的主程序出现延迟,因此我想在另一个线程中执行 SendMail 函数,如下所示:
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");
}
好吧,此时我注意到我需要更改邮件的参数,例如 AdressClient@mail.com、Suject 和 Body。
但我不知道如何:/
如何编写一个接受参数的线程,如下所示:
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);
}
我试过但没有用,我有一个 MethodName Expected 和一个错误通知在这一行:
Thread ThreadSendEmail = new Thread(new ThreadStart(this.SendMail(Body,AdressMail,Subject));
如何编写接受参数的线程?谢谢你的帮助!此致!