Multiple address in MailAddress constructor

36,401

Solution 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("[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]");

Solution 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:

etc...

I wrote more about this topic in this blog post

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
36,401
Vetrivel mp
Author by

Vetrivel mp

12+ years of experience in Asp.net Core, C#, SQL, Angular 4 +, Azure AD B2C, Azure API Management System, Azure Functions, Azure Storage, Azure DevOps, Azure CDN, Azure App Service. Working as Technical Lead in reputed MNC.

Updated on October 30, 2020

Comments

  • Vetrivel mp
    Vetrivel mp over 3 years

    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: ';'
    
  • Vetrivel mp
    Vetrivel mp about 12 years
    what is difference bw mine and yours?
  • Vetrivel mp
    Vetrivel mp about 12 years
    see i know this but how to set in the constructor itself?
  • Chris
    Chris about 12 years
    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).
  • Vetrivel mp
    Vetrivel mp about 12 years
    yes but if i am giving format like "[email protected]<sapce>;<sapce>sample1&google.com". it takes seconde email id and sending mail. is this correct functionality?
  • Vetrivel mp
    Vetrivel mp about 12 years
    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?
  • Massimiliano Peluso
    Massimiliano Peluso about 12 years
    this will work new MailAddress("[email protected]", "Vetrivelmp");
  • Vetrivel mp
    Vetrivel mp about 12 years
    you know why this is happening?
  • Chris
    Chris about 12 years
    No, you can't. A MailAddress object is for a single Mail Address. msdn.microsoft.com/en-us/library/… is the docs that should hopefully answer any other questions you have on the object.
  • Massimiliano Peluso
    Massimiliano Peluso about 12 years
    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
  • Matt Hogan-Jones
    Matt Hogan-Jones almost 10 years
    Would mail.To = "[email protected];[email protected];[email protected]"; 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.
  • snumpy
    snumpy over 9 years
    @MassimilianoPeluso Your first code sample throws an error. The second line should read mail.To.Add("[email protected],[email protected],her@hercomp‌​any.com");
  • Massimiliano Peluso
    Massimiliano Peluso over 9 years
    @MattJones have fixed my answer. Thanks!
  • Massimiliano Peluso
    Massimiliano Peluso over 9 years
    @snumpy I have fixed my answer. Thanks!
  • Adam Miller
    Adam Miller almost 9 years
    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 <[email protected]>"
  • Arkaine55
    Arkaine55 over 7 years
    @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.
  • Judah Gabriel Himango
    Judah Gabriel Himango over 6 years
    That doesn't do what you think it does. That sends an email only to [email protected], and the person's name will appear as [email protected]
  • smoore4
    smoore4 over 6 years
    You are right. It should be like this: "[email protected],[email protected]". And it is a string not a MailAddress.
  • Ralph Willgoss
    Ralph Willgoss about 5 years
    The delimeter is in the documentation now - docs.microsoft.com/en-us/dotnet/api/…