Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple address in MailAddress constructor

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: ';'
like image 803
Vetrivel mp Avatar asked Mar 16 '12 11:03

Vetrivel mp


3 Answers

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("[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]");
like image 187
Massimiliano Peluso Avatar answered Oct 19 '22 00:10

Massimiliano Peluso


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:

etc...

I wrote more about this topic in this blog post

like image 21
Tschareck Avatar answered Oct 19 '22 00:10

Tschareck


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("[email protected], [email protected]");
  ...
}

MailAddressCollection.Add(MailAddress):

using (MailMessage msg = new MailMessage())
{
  ...
  msg.To.Add(new MailAddress("[email protected]", "Vetrivelmp"));
  msg.To.Add(new MailAddress("[email protected]", "Vetrivelmp1"));
  ...
}
like image 6
JohnB Avatar answered Oct 19 '22 00:10

JohnB