简体   繁体   English

SmtpClient.SendAsync()没有收到我发送的所有邮件

[英]SmtpClient.SendAsync() not receiving all messages I sent

I've just written some test code looking at the System.Net.Mail.SmtpClient sending identical messages to myself. 我刚刚编写了一些测试代码,查看System.Net.Mail.SmtpClient向我自己发送相同的消息。

int numClients = 10;
List<SmtpClient> mailClients = new List<SmtpClient>();
for (int i = 0; i < numClients; i++) {
    mailClients.Add(new SmtpClient(smtpHost));
}

MailMessage msg = new MailMessage("myAddress@eg.com", "myAddress@eg.com", "test message", "" );
foreach (SmtpClient c in mailClients) {
    c.SendAsync(msg, null);
}

This is all fine and executes without any problems except that I only receive 'n - 1' messages. 这一切都很好并且执行没有任何问题,只是我只收到'n - 1'消息。 ie If I send 10 messages I only recieve 9 in my inbox. 即如果我发送10条消息,我只在收件箱中收到9条消息。 If I send 50 I only receive 49 etc. 如果我发送50我只收49等

Note: If I change the code to use a blocking Send then I will always receive the right number of messages. 注意:如果我更改代码以使用阻止发送,那么我将始终收到正确数量的消息。 eg 例如

foreach (SmtpClient c in mailClients) {
    c.Send(msg);
} 

Any ideas? 有任何想法吗?

Here are a few observations that may help: 以下是一些可能有用的观察结果:

  • Only create one SmtpClient. 只创建一个SmtpClient。
  • Create multiple messages instead. 改为创建多条消息。
  • SmtpClient implements IDisposable. SmtpClient实现了IDisposable。 Wrap it in using . 包裹它使用
  • MailMessage also implements IDisposable. MailMessage还实现了IDisposable。

I suspect you may be running into a bug/issue with multiple SmtpClient instances that all wrap the same SMTP server. 我怀疑你可能遇到了多个SmtpClient实例的错误/问题,这些实例都包装了同一个SMTP服务器。 Using a single instance may resolve the issue. 使用单个实例可以解决问题。

UPDATE UPDATE

Per MSDN: 每个MSDN:

After calling SendAsync, you must wait for the e-mail transmission to complete before attempting to send another e-mail message using Send or SendAsync. 调用SendAsync后,必须等待电子邮件传输完成后再尝试使用Send或SendAsync发送另一封电子邮件。

http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx

So given your situation, there is almost no benefit to using SendAsync over Send. 因此,根据您的情况,使用SendAsync over Send几乎没有任何好处。 Your loop is probably stomping on something since you do not wait for the previous SendAsync to complete. 您的循环可能正在踩东西,因为您不等待以前的SendAsync完成。

Here are a few thoughts: 以下是一些想法:

  • SendAsync will perform almost the same as Send if you are sending a bunch of emails. 如果您要发送大量电子邮件,SendAsync将执行与Send几乎相同的操作。 Just use Send. 只需使用发送。
  • If you need parallel sending, use a Producer/Consumer pattern. 如果需要并行发送,请使用生产者/消费者模式。 One (or more) producing threads dump stuff into a queue to send, and multiple consuming threads each use one SmtpClient to send messages. 一个(或多个)生成线程将内容转储到队列中以发送,并且多个消费线程各自使用一个SmtpClient来发送消息。 This pattern is amazingly simple to implement with a BlockingCollection. 使用BlockingCollection实现这种模式非常简单。 See the example in MSDN http://msdn.microsoft.com/en-us/library/dd267312.aspx 请参阅MSDN中的示例http://msdn.microsoft.com/en-us/library/dd267312.aspx
  • If you use enough threads, your SMTP server will be the bottleneck. 如果使用足够的线程,则SMTP服务器将成为瓶颈。 Be aware of when you are overloading it. 注意你何时超载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM