31

I am getting this error when I am using it in sharepoint project, while in console app its working fine

I am using MailMessage class to send email using SMTP . But when I trying to add user to 'To' property I am getting {"An invalid character was found in the mail header: ','."} exception, which I think something fishy is happening here as ',' is allowed to separate multiple users . Adding multiple user

** Multiple e-mail addresses must be separated with a comma character (",").**

MailMessage mailMessage = new MailMessage();

 mailMessage.To.Add("[email protected],[email protected],");
nbi
  • 1,276
  • 4
  • 16
  • 26
  • 1
    Possible duplicate? [How to send email to multiple address using System.Net.Mail](http://stackoverflow.com/questions/7498968/how-to-send-email-to-multiple-address-using-system-net-mail) – Izzy Sep 18 '14 at 08:46
  • @Izzy Sorry but I tried to search in stackoverflow but can't find any resolution for this particular scenario i.e MailMessage doesn't accepting ',' character while it's written in MSDN. Its regarding the exception – nbi Sep 18 '14 at 09:00

5 Answers5

49

Got the culprit: It's the extra comma(,) at the end of last email address

mailMessage.To.Add("[email protected],[email protected],");

Just removed that and voila! its working. Don't know why it's working in console application but not in sharepoint :(

mailMessage.To.Add("[email protected],[email protected]");

If this does not work in SharePoint then please add each address separately onto MailMessage object like below;

foreach (var address in StringofEmails.Split(",")) {
MailMessage.To.Add(new MailAddress(address.Trim(), ""));

}

ShahidAliK
  • 30
  • 7
nbi
  • 1,276
  • 4
  • 16
  • 26
14

I got the error even though I don't have a comma at the end. It turns out that I need to leave a space after the comma

I have to change my code from a string.Join(",", emailList) to string.Join(", ", emailList)

Following didn't work for me.

mailMessage.To.Add("[email protected],[email protected]");

Following worked for me(Observe that there is space after the comma).

mailMessage.To.Add("[email protected], [email protected]");
Thiru
  • 407
  • 5
  • 7
2

I can't replicate this. The above code works for me. Maybe try to add them using a seperate 'To' each time.

mailMessage.To.Add(x);
mailMessage.To.Add(y);
Stephen Walker
  • 574
  • 3
  • 10
  • Thanks. It works, but is there any issue in using specified in question ? – nbi Sep 18 '14 at 09:01
  • @Nee I couldn't find one sorry, I copied your code directly into my VS and had no problems with it :? Maybe a VS restart might help, or try adding a space after the comma? – Stephen Walker Sep 18 '14 at 09:04
  • I am getting this error only when I am using it in sharepoint project but not in console application or other – nbi Sep 18 '14 at 09:50
  • 2
    There are classes for Smtp in both System.Web and System.Net. In one you must use comma, in the other you must use semi colon. Don't remember which was which. The one in System.Net is the one that is recommended if I remember correctly... – user1429080 Sep 18 '14 at 15:12
  • 1
    Semi colon doesn't work with System.Net.Mail.SmtpClient – JayJay Dec 07 '15 at 17:40
0

I had to update a project with nicer looking emails and I published the web project and got this error.

Mine was from some debug code in which

currentUser = [email protected]   

got added

MailAddress mailAddressUser = new MailAddress(currentUser + "@mycompany.com");

Essentially:

[email protected]@mycompany.com    

So instead of an issue with a trailing comma, literally another @

Tom Stickel
  • 19,633
  • 6
  • 111
  • 113
0

In my case I forgot to filter out users without email, so I was forming my list of emails like this:

[email protected]
[email protected]
@domain.com  //PROBLEM HERE
[email protected]
The One
  • 4,560
  • 5
  • 36
  • 52