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

13

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14