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: ';'
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: ';'
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]");
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 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 mail.To.Add("[email protected],[email protected],[email protected]");
–
Emmalynn 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 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]
"[email protected]
"[email protected]
, [email protected]
"etc...
I wrote more about this topic in this blog post
"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 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"));
...
}
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.
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 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.
@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
This is what worked for me.
MailMessage m_message = new MailMessage();
string m_addys = "[email protected],[email protected]";
m_message.To.Add(m_addys);
© 2022 - 2024 — McMap. All rights reserved.