Speed up sending multiple emails through smtp server using System.Net.Mail

允我心安 提交于 2019-12-09 03:13:56

问题


I am quite a newbie in C# but I have learnt a lot from VB.Net about programming in .Net for windows.

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.

Is this possible in C#?


回答1:


simply use multiple threads (multiple processes).

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.

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.



来源:https://stackoverflow.com/questions/20747498/speed-up-sending-multiple-emails-through-smtp-server-using-system-net-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!