EDIT: This question is pointless, except as an exercise in red herrings. The issue turned out to be a combination of my idiocy (NO ONE was being emailed as the host was not being specified and was incorrect in web.config) and the users telling me that they sometimes got the emails and sometimes didn’t, when in reality they were NEVER getting the emails.

So, instead of taking proper steps to reproduce the problem in a controlled setting, I relied on user information and the “it works on my machine” mentality. Good reminder to myself and anyone else out there who is sometimes an idiot.


我只是碰到了一些我认为不一致的东西,想看看我是否做错了什么,如果我是个白痴,或者……

MailMessage msg = new MailMessage();
msg.To.Add("[email protected]");
msg.To.Add("[email protected]");
msg.To.Add("[email protected]");
msg.To.Add("[email protected]");

实际上只将这封电子邮件发送给最后一个人。

要添加倍数,我必须这样做:

msg.To.Add("[email protected],[email protected],[email protected],[email protected]");

我不明白。To地址收集,但我正在做的是替换它。

我想我刚刚意识到我的错误 - 要将一项添加到集合中,请使用 .To.Add(new MailAddress(“[email protected]”))

如果你只使用一个string,它会替换列表中的所有内容。EDIT: Other people have tested and are not seeing this behavior. This is either a bug in my particular version of the framework, or more likely, an idiot maneuver by me.

啊。

答案

我无法复制你的错误:

var message = new MailMessage();

message.To.Add("[email protected]");
message.To.Add("[email protected]");

message.From = new MailAddress("[email protected]");
message.Subject = "Test";
message.Body = "Test";

var client = new SmtpClient("localhost", 25);
client.Send(message);

转储 To 的内容:邮件地址集合

邮件地址集合 (2 项)
显示名称 用户主机地址

用户 example.com [email protected]
user2 example.com [email protected]

结果电子邮件被捕获smtp4dev

Received: from mycomputername (mycomputername [127.0.0.1])
     by localhost (Eric Daugherty's C# Email Server)
     3/8/2010 12:50:28 PM
MIME-Version: 1.0
From: [email protected]
To: [email protected], [email protected]
Date: 8 Mar 2010 12:50:28 -0800
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Test

Are you sure there’s not some other issue going on with your code or SMTP server?

来自: stackoverflow.com