[英]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: 以下是一些可能有用的观察结果:
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: 以下是一些想法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.