C# MailAddress 构造函数中的多个地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9736176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:38:12  来源:igfitidea点击:

Multiple address in MailAddress constructor

c#.netsystem.net.mailmailmessage

提问by Vetrivel mp

i was trying to add multiple to address like this.

我试图添加多个来解决这样的问题。

MailAddress mailAddressTo = new MailAddress("[email protected];[email protected]","Vetrivelmp");

but throws error like

但抛出错误,如

An invalid character was found in the mail header: ';'

采纳答案by Massimiliano Peluso

You cannot use the MailAddressconstructor for specifying multiple receipts, but you can to use the MailMessageobject as showed below.

您不能使用MailAddress构造函数来指定多个收据,但您可以使用MailMessage如下所示的对象。

Using the MailMessage(notMailAddress) constructor:

使用MailMessage( notMailAddress) 构造函数:

var msg = new MailMessage("[email protected]", "[email protected], [email protected]");

another way is:

另一种方法是:

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

another way is:

另一种方法是:

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

回答by Chris

There might be a question of why you are wanting to do this? Something like MailMessage.Tois a MailAddressCollectionwhose Addmethod is overloaded to take multiple e-mail addresses in a string, separated by a comma (see http://msdn.microsoft.com/en-us/library/ms144695.aspx).

可能会有一个问题,你为什么要这样做?喜欢的东西MailMessage.To是一个MailAddressCollectionAdd方法是重载采取多个电子邮件地址中的字符串,分隔用逗号(见http://msdn.microsoft.com/en-us/library/ms144695.aspx)。

The usual use for MailAddress objects is to add them to e-mails and if you have multiple addresses then I assume you want to add them to one of the To, CC etc. fields in which case the Add overload should do you nicely. If there is something else then you are going to have to provide more context for what you are trying to do.

MailAddress 对象的通常用途是将它们添加到电子邮件中,如果您有多个地址,那么我假设您想将它们添加到 To、CC 等字段之一,在这种情况下,Add 重载应该很适合您。如果还有其他事情,那么您将不得不为您尝试做的事情提供更多背景信息。

回答by Tschareck

Actually, semicolon is not a valid delimiter. Unfortunately, MSDN does not document this, had to find out this by myself.

实际上,分号不是有效的分隔符。不幸的是,MSDN 没有记录这个,必须自己找出来。

If you want to add more addresses, divide them by comma. And the space will divide display name and email address. The "To" property accepts following formats:

如果要添加更多地址,请用逗号分隔它们。并且该空间将划分显示名称和电子邮件地址。“To”属性接受以下格式:

etc...

等等...

I wrote more about this topic in this blog post

在这篇博文中写了更多关于这个主题的文章

回答by Anarud

@Tschareck

@Tschareck

"A comma is used to separate elements in a list of mail addresses. As a result, a comma should not be used in unquoted display names in a list. The following mail addresses would be allowed" in http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx

“逗号用于分隔邮件地址列表中的元素。因此,不应在列表中不带引号的显示名称中使用逗号。以下邮件地址将被允许”在http://msdn.microsoft 中。 com/en-us/library/system.net.mail.mailaddress.aspx

Best regards, Anarud

最好的问候,阿纳鲁德

回答by JohnB

Use a comma (,) as the separator instead of semicolon (;).

使用逗号 (,) 作为分隔符而不是分号 (;)。

If multiple e-mail addresses separated with a semicolon character (";") are passed in the addresses parameter. a FormatException exception is raised.

如果在addresses 参数中传递以分号字符(“;”)分隔的多个电子邮件地址。引发 FormatException 异常。

Examples that work

有效的例子

MailAddressCollection.Add(String):

MailAddressCollection.Add(String):

using (MailMessage msg = new MailMessage())
{
  ...
  msg.To.Add("[email protected], [email protected]");
  ...
}

MailAddressCollection.Add(MailAddress):

MailAddressCollection.Add(MailAddress):

using (MailMessage msg = new MailMessage())
{
  ...
  msg.To.Add(new MailAddress("[email protected]", "Vetrivelmp"));
  msg.To.Add(new MailAddress("[email protected]", "Vetrivelmp1"));
  ...
}

回答by smoore4

This is what worked for me.

这对我有用。

  MailMessage m_message = new MailMessage();
  string m_addys = "[email protected],[email protected]";
  m_message.To.Add(m_addys);

回答by Guest

Here's another variation on this theme, FWIW:

这是这个主题的另一个变体,FWIW:

    SenderEmail = "[email protected]";
    RecipientEmail = "[email protected], [email protected], [email protected]";
    MailMessage msg = new MailMessage(SenderEmail, RecipientEmail);

Note the commas. Further details can be found at MSDN here.

注意逗号。可以在 MSDN此处找到更多详细信息。