我想在控制台应用程序中使用 smtp 一次发送多个电子邮件请求。如何使用线程一次发送批量电子邮件请求
I want to send multiple email request at a time using smtp in console application .how I can send bulk email request at a time with using threading
for (int i = 0; i < 50; i++)
{
Thread T1 = new Thread(delegate ()
{
Console.WriteLine("email sent..");
smtp.Send(msg);
});
T1.Start();
}
我正在尝试使用 smtp.Send(msg).
通过多线程发送批量请求
我已经尝试了上面的代码,但我收到了这个错误
An asynchronous call is already in progress. It must be completed or canceled before you can call this method
我该如何解决这个问题。
如果您正在使用以下client。我建议您重构您的代码,如下所示:
var tasks = new List<Task>()
for (int i = 0; i < 50; i++)
{
task.Add(smtp.SendAsync(msg));
}
await Task.WhenAll(tasks)
有什么区别?
使用任务将在后台使用线程,但会负责所有线程调度和清理。如果您想为应用程序添加并行性和并发性,c# TPL 是必经之路(我从上面偷了那行 link)
考虑到@phuzi 对您问题的评论,您可能需要在 for 循环中实例化一个客户端。
请记住,这将使用 await,因为您必须将 async 添加到您的函数签名中。
for (int i = 0; i < 50; i++)
{
Thread T1 = new Thread(delegate ()
{
Console.WriteLine("email sent..");
smtp.Send(msg);
});
T1.Start();
}
我正在尝试使用 smtp.Send(msg).
通过多线程发送批量请求我已经尝试了上面的代码,但我收到了这个错误
An asynchronous call is already in progress. It must be completed or canceled before you can call this method
我该如何解决这个问题。
如果您正在使用以下client。我建议您重构您的代码,如下所示:
var tasks = new List<Task>()
for (int i = 0; i < 50; i++)
{
task.Add(smtp.SendAsync(msg));
}
await Task.WhenAll(tasks)
有什么区别?
使用任务将在后台使用线程,但会负责所有线程调度和清理。如果您想为应用程序添加并行性和并发性,c# TPL 是必经之路(我从上面偷了那行 link)
考虑到@phuzi 对您问题的评论,您可能需要在 for 循环中实例化一个客户端。
请记住,这将使用 await,因为您必须将 async 添加到您的函数签名中。