问题描述
我是 C# 的新手,但我从 VB.Net 学到了很多关于在 .Net for windows 中编程的知识.
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# 中可行吗?
Is this possible in C#?
推荐答案
只需使用多线程(多进程)即可.
simply use multiple threads (multiple processes).
在 C# 中,您可以使用任务来完成此操作.
In C# you can do this with a Task.
new Task(delegate {
smtpClient.send(myMessage);
}).Start();
只需将您的 send
命令包装在此对象中,它将异步发送.
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.
如果您需要同时发送大量邮件,我建议您使用ThreadPool
.它使您可以控制要同时拥有多少并发线程.
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.
这篇关于使用 System.Net.Mail 加速通过 smtp 服务器发送多封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!