使用 System.Net.Mail 加速通过 smtp 服务器发送多封电子邮件

Speed up sending multiple emails through smtp server using System.Net.Mail(使用 System.Net.Mail 加速通过 smtp 服务器发送多封电子邮件)
本文介绍了使用 System.Net.Mail 加速通过 smtp 服务器发送多封电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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 服务器发送多封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)