10

I'm using System.Net.Mail to send email in my application but I get an exception and I can't figure out what/where the problem is and how to fix it.

The error says I have some invalid char:

An invalid character was found in the mail header: ';'.

I tried google without success.

The string with email address is:

john@mydomain.org; beth@mydomain.org; alfred@mydomain.org; barbie@mydomain.org; 

Here is my email sending code:

    SmtpClient smtpClient = new SmtpClient("smtp.........");
    System.Net.Mail.MailMessage mailMessagePlainText = new System.Net.Mail.MailMessage();

    mailMessagePlainText.IsBodyHtml = true;
    mailMessagePlainText.From = new MailAddress("vincent@mydomain.org", "admin");

    mailMessagePlainText.Subject = "test";
    mailMessagePlainText.Body = "test";

    mailMessagePlainText.To.Add(new MailAddress(List1.ToString(), ""));
    mailMessagePlainText.Bcc.Add(new MailAddress("vincent@mydomain.org", ""));

    try
    {
        smtpClient.Send(mailMessagePlainText);
    }
    catch (Exception ex)
    {
        throw (ex);
    }
Dale K
  • 25,246
  • 15
  • 42
  • 71
Antonio Mailtraq
  • 1,397
  • 5
  • 34
  • 82
  • possible duplicate of [Sending Email to multiple Recipients with MailMessage](http://stackoverflow.com/questions/23484503/sending-email-to-multiple-recipients-with-mailmessage) – PMerlet Jul 01 '15 at 07:30
  • What is the value of List1? Is it the email address string you mentioned? – MeanGreen Jul 01 '15 at 07:31
  • @MeanGreen: Yes Sir the value of List1 is it the email address string: john@mydomain.org; beth@mydomain.org; alfred@mydomain.org; barbie@mydomain.org; – Antonio Mailtraq Jul 01 '15 at 07:32
  • Just a suggestion you could try explicitly removing it using either the Trim() or .Replace() methods – Litisqe Kumar Jul 01 '15 at 07:37
  • The use of ; is not valid in SMTP; they need to be separated with commas. You should have a list of such strings (not a single string) and then add them individually to let the SMTP library handle it for you. – AlBlue Jul 01 '15 at 08:56

3 Answers3

10
foreach (var address in List1.split(';')) {
    mailMessagePlainText.To.Add(new MailAddress(address.Trim(), ""));
}

Because according to your string here above, each address in this loop above would produce following:

"john@mydomain.org"
" beth@mydomain.org"
" alfred@mydomain.org"
" barbie@mydomain.org"

So by adding .Trim() to address would make your code work.

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • The parameter 'address' cannot be an empty string. Parameter name: address – Antonio Mailtraq Jul 01 '15 at 09:29
  • The reason for the "empty string" error is that the string of email addresses is suffixed with a semi-colon. Ergo the `Split()` method will see the empty space after the semi-colon as a perfectly valid empty string. However when this is passed into the constructor of `MailAddress` (in the final interation of the `foreach` loop) it fails the validation of an email address. Fix - remove the ending semi-colon. I know this is an old post but anyone experiencing this issue may find the above answer to not be sufficient without this fix. – Kallum Tanton Mar 31 '17 at 10:10
2

A MailAddressCollection (like your mailMessagePlainText.To) has an Add method that accepts a string containing a list of mail addresses, separated by a comma.

So to use that, you will need to change the ; into a , and possibly remove the extra spaces.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
1

It looks like you're adding the addresses as a single MailAddress, where you need to add them 1 at a time. I don't know what other overloads are available, but the following will probably work.

I split the string by ; and add each address separately.

replace

mailMessagePlainText.To.Add(new MailAddress(List1.ToString(), ""));

with

foreach (var address in List1.split(';')) {
    mailMessagePlainText.To.Add(new MailAddress(address , ""));
}
MeanGreen
  • 3,098
  • 5
  • 37
  • 63
  • thank you but now I have ths new exception: The specified string is not in the form required for an e-mail address. – Antonio Mailtraq Jul 01 '15 at 07:41
  • As Hans Kesting mentioned in his answer (I don't have access to the API right now) the character should be a , and not a ; – MeanGreen Jul 01 '15 at 08:26