سؤال

i was trying to add multiple to address like this.

MailAddress mailAddressTo = new MailAddress("sample@google.com;sample1@google.com","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("from@domain.com", "to1@gmail.com, to2@gmail.com");

another way is:

MailMessage mail = new MailMessage();
mail.To.Add("me@mycompany.com,him@hiscompany.com,her@hercompany.com");

another way is:

MailMessage msg = new MailMessage();
msg.To.Add("person1@domain.com");
msg.To.Add("person2@domain.com");
msg.To.Add("person3@domain.com");
msg.To.Add("person4@domain.com");

نصائح أخرى

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@server.com"
  • "email1@server1.com, email2@server2.com"
  • "Name email@server.com"
  • "name email@server1.com, email@server2.com"

etc...

I wrote more about this topic in this blog post

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("sample@google.com, sample1@google.com");
  ...
}

MailAddressCollection.Add(MailAddress):

using (MailMessage msg = new MailMessage())
{
  ...
  msg.To.Add(new MailAddress("sample@google.com", "Vetrivelmp"));
  msg.To.Add(new MailAddress("sample1@google.com", "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.

Here's another variation on this theme, FWIW:

    SenderEmail = "me@mine.com";
    RecipientEmail = "this@this.com, that@that.com, other@theother.com";
    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 = "addy2@foo.com,addy1@foo.com";
  m_message.To.Add(m_addys);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top