Multiple address in MailAddress constructor
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:
[email protected]
- "
[email protected]
,[email protected]
" - "Name
[email protected]
" - "name
[email protected]
,[email protected]
"
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.
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, 2020Comments
-
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 about 12 yearswhat is difference bw mine and yours?
-
Vetrivel mp about 12 yearssee i know this but how to set in the constructor itself?
-
Chris about 12 yearsHis 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 aMailAddressCollection
that allows multiple e-mail addresses.MailAddress
only ever holds a single address (the fact its singular may help you remember this). -
Vetrivel mp about 12 yearsyes 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 about 12 yearsreason 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 about 12 yearsthis will work new MailAddress("[email protected]", "Vetrivelmp");
-
Vetrivel mp about 12 yearsyou know why this is happening?
-
Chris about 12 yearsNo, 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 about 12 yearseven 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 almost 10 yearsWould
mail.To = "[email protected];[email protected];[email protected]";
really work?MailMessage.To
is aMailAddressCollection
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 over 9 years@MassimilianoPeluso Your first code sample throws an error. The second line should read
mail.To.Add("[email protected],[email protected],her@hercompany.com");
-
Massimiliano Peluso over 9 years@MattJones have fixed my answer. Thanks!
-
Massimiliano Peluso over 9 years@snumpy I have fixed my answer. Thanks!
-
Adam Miller almost 9 yearsIn 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 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 over 6 yearsThat 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 over 6 yearsYou are right. It should be like this: "[email protected],[email protected]". And it is a string not a MailAddress.
-
Ralph Willgoss about 5 yearsThe delimeter is in the documentation now - docs.microsoft.com/en-us/dotnet/api/…