简体   繁体   English

MailAddress 构造函数中的多个地址

[英]Multiple address in MailAddress constructor

i was trying to add multiple to address like this.我试图添加多个这样的地址。

MailAddress mailAddressTo = new MailAddress("sample@example.com;sample1@example.com","Vetrivelmp");

but throws error like但会抛出错误

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

You cannot use the MailAddress constructor for specifying multiple receipts, but you can to use the MailMessage object as showed below.您不能使用MailAddress构造函数来指定多个收据,但您可以使用MailMessage对象,如下所示。

Using the MailMessage ( not MailAddress ) constructor:使用MailMessage不是MailAddress )构造函数:

var msg = new MailMessage("from@domain.example", "to1@gmail.com, to2@gmail.com");

another way is:另一种方法是:

MailMessage mail = new MailMessage();
mail.To.Add("me@mycompany.example,him@hiscompany.example,her@hercompany.example");

another way is:另一种方法是:

MailMessage msg = new MailMessage();
msg.To.Add("person1@domain.example");
msg.To.Add("person2@domain.example");
msg.To.Add("person3@domain.example");
msg.To.Add("person4@domain.example");

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”属性接受以下格式:

  • email@server.example
  • " email1@server1.example , email2@server2.example " " email1@server1.example , email2@server2.example "
  • "Name email@server.example " “名称email@server.example
  • "name email@server1.example , email@server2.example " "名称email@server1.example , email@server2.example "

etc... ETC...

I wrote more about this topic in this blog post在这篇博文中写了更多关于这个主题的文章

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.如果以分号字符 (";") 分隔的多个电子邮件地址在addresses 参数中传递。 a FormatException exception is raised.引发 FormatException 异常。

Examples that work有效的例子

MailAddressCollection.Add(String): MailAddressCollection.Add(字符串):

using (MailMessage msg = new MailMessage())
{
  ...
  msg.To.Add("sample@example.com, sample1@example.com");
  ...
}

MailAddressCollection.Add(MailAddress): MailAddressCollection.Add(MailAddress):

using (MailMessage msg = new MailMessage())
{
  ...
  msg.To.Add(new MailAddress("sample@example.com", "Vetrivelmp"));
  msg.To.Add(new MailAddress("sample1@example.com", "Vetrivelmp1"));
  ...
}

There might be a question of why you are wanting to do this?可能会有一个问题,您为什么要这样做? Something like MailMessage.To is a MailAddressCollection whose Add method 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之类的东西是MailAddressCollection ,它的Add方法被重载以在字符串中获取多个电子邮件地址,用逗号分隔(请参阅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. MailAddress 对象的通常用途是将它们添加到电子邮件中,如果您有多个地址,那么我假设您想将它们添加到 To、CC 等字段之一,在这种情况下,Add 重载应该很好。 If there is something else then you are going to have to provide more context for what you are trying to do.如果还有其他事情,那么您将不得不为您正在尝试做的事情提供更多上下文。

Here's another variation on this theme, FWIW:这是这个主题的另一个变体,FWIW:

    SenderEmail = "me@mine.example";
    RecipientEmail = "this@this.example, that@that.example, other@theother.example";
    MailMessage msg = new MailMessage(SenderEmail, RecipientEmail);

Note the commas.注意逗号。 Further details can be found at MSDN here .更多详细信息可在此处的 MSDN 中找到。

@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最好的问候, 阿纳鲁德

This is what worked for me.这对我有用。

  MailMessage m_message = new MailMessage();
  string m_addys = "addy2@example.com,addy1@example.com";
  m_message.To.Add(m_addys);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这个构造函数在做什么? 新的MailAddress [0] - What is this constructor doing? new MailAddress[0] MailAddress构造函数如何验证邮件地址 - How does the MailAddress constructor validate mail addresses 将 C# 中的 email 地址字符串解析为 MailAddress object - Parse an email address string in C# to MailAddress object 为什么MailAddress不拒绝电子邮件地址someName @ gmail? - Why does MailAddress not reject the email address someName@gmail? 是“ a @ bbb..com”有效的电子邮件地址吗?,根据MailAddress类,它是 - Is it “a@bbb..com” a valid email address?, According to MailAddress class it is 为什么 MailAddress 认为“john@gmail”。 是一个有效的电子邮件地址吗? - Why does MailAddress think 'john@gmail.' is a valid email address? System.Net.Mail.MailAddress使用一个参数发送到多封电子邮件 - System.Net.Mail.MailAddress send to multiple emails with one parameter 为什么 System.Net.Mail.MailAddress 构造函数在域部分解析带有斜杠“/”的电子邮件? - Why System.Net.Mail.MailAddress constructor parses email with slash "/" in domain part? 创建新的MailAddress会产生“指定的字符串不是电子邮件地址所需的格式”,尽管它的格式正确 - Creating a new MailAddress produces “The specified string is not in the form required for an e-mail address”, although it is in the correct format MailMessage使用字符串代替To中的MailAddress - MailMessage with string instead of MailAddress in To
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM