32

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: ';'
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Vetrivel mp
  • 1,214
  • 1
  • 14
  • 29

7 Answers7

47

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]");
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
21

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Tschareck
  • 4,071
  • 9
  • 47
  • 74
  • 1
    In your last example, `"name [email protected], [email protected]"`, the MailMessage was interpreting a test I did as "name email" as the email prefix. I had to do it like this: `"Adam Miller "` – Adam Miller May 28 '15 at 18:40
  • The delimeter is in the documentation now - https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.-ctor?view=netframework-4.7.2#System_Net_Mail_MailMessage__ctor_System_String_System_String_System_String_System_String_ – Ralph Willgoss Mar 26 '19 at 11:30
6

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.

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"));
  ...
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
JohnB
  • 18,046
  • 16
  • 98
  • 110
2

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.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • reason is i have predefined code that i am not supposed to change. so is that possible to add multiple ids inside mailaddress consturctor or not? – Vetrivel mp Mar 16 '12 at 11:21
  • No, you can't. A `MailAddress` object is for a single Mail Address. http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx is the docs that should hopefully answer any other questions you have on the object. – Chris Mar 16 '12 at 11:25
1

Here's another variation on this theme, 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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Guest
  • 11
  • 1
0

@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

Anarud
  • 126
  • 3
-2

This is what worked for me.

  MailMessage m_message = new MailMessage();
  string m_addys = "[email protected],[email protected]";
  m_message.To.Add(m_addys);
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
smoore4
  • 4,520
  • 3
  • 36
  • 55