32

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: ';'
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("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");
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • what is difference bw mine and yours? – Vetrivel mp Mar 16 '12 at 11:12
  • see i know this but how to set in the constructor itself? – Vetrivel mp Mar 16 '12 at 11:13
  • His has a MailMessage object for a start. I've actually explained a bit of this in my answer. the simple answer is that `mail.To` is a `MailAddressCollection` that allows multiple e-mail addresses. `MailAddress` only ever holds a single address (the fact its singular may help you remember this). – Chris Mar 16 '12 at 11:14
  • yes but if i am giving format like "sample@google.com;sample1&google.com". it takes seconde email id and sending mail. is this correct functionality? – Vetrivel mp Mar 16 '12 at 11:19
  • this will work new MailAddress("sample@google.com", "Vetrivelmp"); – Massimiliano Peluso Mar 16 '12 at 11:21
  • you know why this is happening? – Vetrivel mp Mar 16 '12 at 11:23
  • even if there is an issue with MailAdress object you have to use the MailMessage for multiple address as MailAddress is for only one address – Massimiliano Peluso Mar 16 '12 at 11:28
  • 1
    Would `mail.To = "me@mycompany.com;him@hiscompany.com;her@hercompany.com";` really work? `MailMessage.To` is a `MailAddressCollection` object. You can't assign a string to it - you'd need to use the `.Add` method and your list of email addresses would have to be separated by commas, not semi-colons. – Matt Hogan-Jones Jul 08 '14 at 16:46
  • @MassimilianoPeluso Your first code sample throws an error. The second line should read `mail.To.Add("me@mycompany.com,him@hiscompany.com,her@hercompany.com");` – snumpy Dec 09 '14 at 21:22
  • @AdamMiller I think this answer addresses the question about multiple addresses in a `MailAddress` constructor better. But I agree the answer by Tscharek is more correct because it explains using semicolon as the address delimiter is wrong no matter what. – Arkaine55 Sep 20 '16 at 14:53
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:

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

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@server1.com, email@server2.com"`, 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("sample@example.com, sample1@example.com");
  ...
}

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"));
  ...
}
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 = "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.

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 = "addy2@example.com,addy1@example.com";
  m_message.To.Add(m_addys);
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
smoore4
  • 4,520
  • 3
  • 36
  • 55