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("person1@domain.com");
msg.To.Add("person2@domain.com");
msg.To.Add("person3@domain.com");
msg.To.Add("person4@domain.com");

真的只发送这封邮箱给1个人,最后一个人.

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

msg.To.Add("person1@domain.com,person2@domain.com,person3@domain.com,person4@domain.com");

我还是不明白.我以为我要将多个人添加到To个地址集合中,但我所做的是替换它.

我想我刚刚意识到了我的错误——要向集合中添加一个项目,请使用

如果你只使用string,它将替换列表中的所有内容.

啊.我认为这是一个相当大的trap !既然我回答了自己的问题,但我认为这在stackoverflow存档中是有价值的,我还是会问.也许有人甚至想到了你可能会落入的其他trap .

推荐答案

我无法复制你的错误:

var message = new MailMessage();

message.To.Add("user@example.com");
message.To.Add("user2@example.com");

message.From = new MailAddress("test@example.com");
message.Subject = "Test";
message.Body = "Test";

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

将内容转储到:MailAddressCollection:

MailAddressCollection(2项)

user example.com user@example.com
user2 example.com user2@example.com

而由此产生的邮箱被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: test@example.com
To: user@example.com, user2@example.com
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?

Asp.net相关问答推荐

无法从部署在应用程序负载均衡器后面的 AWS ECS 上的应用程序发送 HTTP/S 请求

如何在编译时为我的 ASP.NET 项目中的每个控制器生成一个单独的 OpenAPI Swagger.json 文件?

如何在 MVC 中为国家和州/省创建 Select 列表

如何在 Javascript 中获取 C# 枚举

InvalidOperationException:在程序集上找不到UserSecretsIdAttribute

在正则表达式中允许 -(破折号)

有条件地在 Gridview 中隐藏 CommandField 或 ButtonField

我应该使用字节还是整数?

正在检索组件的 COM 类工厂......错误:80070005 访问被拒绝. (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))

.NET 核心 IdentityUser 模型中规范化邮箱和用户名的用途是什么?

在 C# 中将 IHtmlContent/TagBuilder 转换为字符串

如何配置 ASP.NET Core 1.0 以使用本地 IIS 而不是 IIS Express?

如何将 ASP.NET MVC5 身份验证添加到现有数据库

带有邮箱地址参数的 WebApi 方法从 HttpClient 返回 404

如何从 JS 访问 ViewBag

.NET - c# - 需要跨分区查询,但在 DocumentDB 数据访问上禁用了问题

WebForm_PostBackOptions 文档

C# - 将图像输出到响应输出流给出 GDI+ 错误

elmah:没有 HttpContext 的异常?

盒式磁带包与 MVC4 包