Multiple address in MailAddress constructor

2019-02-07 22:01发布

问题:

i was trying to add multiple to address like this.

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

but throws error like

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

回答1:

You cannot use the MailAddress constructor for specifying multiple receipts, but you can to use the MailMessage object as showed below.

Using the MailMessage (not MailAddress) constructor:

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

another way is:

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

another way is:

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");


回答2:

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

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:

  • "email@server.com"
  • "email1@server1.com, email2@server2.com"
  • "Name email@server.com"
  • "name email@server1.com, email@server2.com"

etc...

I wrote more about this topic in this blog post



回答3:

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).

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.



回答4:

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.

  • MailAddressCollection.Add(String) Method

Examples that work

MailAddressCollection.Add(String):

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

MailAddressCollection.Add(MailAddress):

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


回答5:

Here's another variation on this theme, FWIW:

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

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



回答6:

@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

Best regards, Anarud



回答7:

This is what worked for me.

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