Reputation: 1224
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: ';'
Upvotes: 33
Views: 42214
Reputation: 1
Below is a good working example.
if (!string.IsNullOrEmpty(ToAddress.TrimEnd(';')))
{
foreach (MailAddress mail_adres in GetEmailAdressen(ToAddress))
{
message.To.Add(mail_adres);
}
}
private static MailAddressCollection GetEmailAdressen(string Address)
{
MailAddressCollection response = new MailAddressCollection();
foreach (var mail_adres in Address.TrimEnd(';').Split(';'))
{
if (mail_adres.Trim().Length > 0)
{
try
{
response.Add(new MailAddress(mail_adres));
}
catch (Exception)
{
// no action, skip the wrong data
}
}
}
return response;
}
Upvotes: -1
Reputation: 4866
This is what worked for me.
MailMessage m_message = new MailMessage();
string m_addys = "[email protected],[email protected]";
m_message.To.Add(m_addys);
Upvotes: -2
Reputation: 11
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.
Upvotes: 1
Reputation: 19012
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"));
...
}
Upvotes: 7
Reputation: 4239
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
Upvotes: 20
Reputation: 26737
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]");
Upvotes: 49
Reputation: 126
@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
Upvotes: 0
Reputation: 27619
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.
Upvotes: 2