Multiple address in MailAddress constructor
Asked Answered
B

7

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: ';'
Bimonthly answered 16/3, 2012 at 11:8 Comment(0)
I
48

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]");
Insertion answered 16/3, 2012 at 11:9 Comment(10)
what is difference bw mine and yours?Bimonthly
see i know this but how to set in the constructor itself?Bimonthly
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).Ratha
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?Bimonthly
this will work new MailAddress("[email protected]", "Vetrivelmp");Insertion
you know why this is happening?Bimonthly
even if there is an issue with MailAdress object you have to use the MailMessage for multiple address as MailAddress is for only one addressInsertion
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.Gendarmerie
@MassimilianoPeluso Your first code sample throws an error. The second line should read mail.To.Add("[email protected],[email protected],[email protected]");Emmalynn
@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.Veg
L
20

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

Lanfri answered 22/3, 2012 at 9:35 Comment(2)
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]>"Descry
The delimeter is in the documentation now - learn.microsoft.com/en-us/dotnet/api/…Corissa
T
7

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"));
  ...
}
Tilla answered 23/11, 2013 at 0:22 Comment(0)
R
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.

Ratha answered 16/3, 2012 at 11:13 Comment(2)
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?Bimonthly
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.Ratha
G
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.

Galsworthy answered 23/12, 2013 at 18:34 Comment(0)
D
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

Diehl answered 20/6, 2013 at 9:17 Comment(0)
P
-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);
Plunder answered 4/12, 2013 at 18:48 Comment(2)
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]Lakin
You are right. It should be like this: "[email protected],[email protected]". And it is a string not a MailAddress.Plunder

© 2022 - 2024 — McMap. All rights reserved.