问题描述
我在C#中不少新手,但我学到了很多,从VB.Net关于编程的。Net的窗口。
I am quite a newbie in C# but I have learnt a lot from VB.Net about programming in .Net for windows.
我刚才提出它发送邮件从程序简单的SMTP客户端。这是一个控制台应用程序,并通过服务器在同一时间只能发送一个电子邮件。这是很慢的,我需要通过我的客户在的同时的发送多封电子邮件。
I have just made a simple SMTP client which sends emails from the program. It is a console application and can only send one email through the server at a time. This is very slow and I need to send multiple emails through my client at the same time.
这是可能在C#?
推荐答案
简单地使用多线程(多进程)。
simply use multiple threads (multiple processes).
在C#中,你可以用一个任务做到这一点。
In C# you can do this with a Task.
new Task(delegate {
smtpClient.send(myMessage);
}).Start();
只是包装你的在这个对象发送
命令,它会发送异步的。
Just wrap your send
command in this object and it will be send Asynchronously.
要小心,如果这个被包裹在一个循环中,将开始一个新的进程为每个邮件。
Be careful if this is wrapped in a loop it will start a new process for each mail.
如果您需要在同一时间发送大量的邮件,我建议你使用线程池
。它可以让你控制,你想有多少concurent线程都在同一时间。
if you need to send large amounts of mails at the same time I suggest you use a ThreadPool
. It lets you control how many concurent threads you'd like to have at the same time.
这篇关于加快通过SMTP服务器发送多封邮件使用System.Net.Mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!