.Net System.Mail.Message添加多个“收件人"地址 [英] .Net System.Mail.Message adding multiple "To" addresses

查看:450
本文介绍了.Net System.Mail.Message添加多个“收件人"地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题没有意义,除了作为红色鲱鱼的练习之外.结果是我的愚蠢(由于没有指定主机,并且在web.config中不正确,因此没有人通过电子邮件发送电子邮件)和用户告诉我,他们有时收到电子邮件,有时没有收到电子邮件,实际上,他们从来没有收到过电子邮件.

因此,我没有采取适当的步骤来在受控设置下重现该问题,而是依靠用户信息和在我的机器上有效"的思想. 很好地提醒自己和有时是个白痴的其他人.

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.

我只是撞了一些我认为不一致的东西,想看看我做错了什么,如果我是个白痴,还是...

I just hit something I think is inconsistent, and wanted to see if I'm doing something wrong, if I'm an idiot, or...

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个人,即最后一个.

Really only sends this email to 1 person, the last one.

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

To add multiples I have to do this:

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

我不明白.我以为我要在To地址集合中添加多个人,但是我正在替换它.

I don't get it. I thought I was adding multiple people to the To address collection, but what I was doing was replacing it.

我想我刚刚意识到自己的错误-向收藏夹中添加一项,使用 .To.Add(新MailAddress("person@domain.com"))

I think I just realized my error -- to add one item to the collection, use .To.Add(new MailAddress("person@domain.com"))

如果仅使用string,它将替换其列表中的所有内容. 其他人已经测试并且没有看到此行为.这要么是我特定版本的框架中的错误,要么更可能是我的愚蠢行为.

If you use just a string, it replaces everything it had in its list. 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.

U.我认为这是一个很大的陷阱!既然我回答了我自己的问题,但是我认为这在stackoverflow存档中很有价值,所以我仍然会问它.也许有人甚至想到了您可能会遇到的其他陷阱.

Ugh. I'd consider this a rather large gotcha! Since I answered my own question, but I think this is of value to have in the stackoverflow archive, I'll still ask it. Maybe someone even has an idea of other traps that you can fall into.

推荐答案

我无法复制您的错误:

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个项目)
DisplayName用户主机地址

MailAddressCollection (2 items)
DisplayName User Host Address

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

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

以及 smtp4dev 捕获的结果电子邮件:

And the resulting e-mail as caught by 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

您确定代码或SMTP服务器没有其他问题吗?

这篇关于.Net System.Mail.Message添加多个“收件人"地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆