MailMessage.To.Add() throwing exception : "An invalid character was found in the mail header: ','."
Asked Answered
A

5

34

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],");
Atticism answered 18/9, 2014 at 8:37 Comment(2)
Possible duplicate? How to send email to multiple address using System.Net.MailSelfmastery
@Selfmastery 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 exceptionAtticism
A
52

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

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

Remove the trailing comma and it will work:

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(), ""));
}
Atticism answered 19/9, 2014 at 3:43 Comment(1)
If you recipients is in a collection you could use string recipients = String.Join(",", _config.NotificationSettings.MailSettings.Recipients);Nikaniki
T
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]");
Tenant answered 16/8, 2018 at 17:29 Comment(3)
This was my issue after I upgraded from .Net 3.5 to .Net 4.7.2. They must have introduced stricter parsing rules.Harleyharli
Placing the space would mean the first is the email ([email protected],) and the second is the name([email protected]). I dont think it would send it to the second person([email protected]). Did you confirm it sent to both emails?Fitzger
Hey @RobertSmith, Yes it worked when I tried back then using .net framework 4.6+. I didn’t tried it recently.Tenant
B
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);
Boliviano answered 18/9, 2014 at 8:42 Comment(5)
Thanks. It works, but is there any issue in using specified in question ?Atticism
@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?Boliviano
I am getting this error only when I am using it in sharepoint project but not in console application or otherAtticism
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...Eijkman
Semi colon doesn't work with System.Net.Mail.SmtpClientTyika
N
-1

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 @

Noria answered 22/12, 2015 at 7:59 Comment(0)
M
-1

you can do something like this as well. This approach also validates the email address before adding it to the dl list. So you will only valid dl list.

var emails = ("[email protected],[email protected],").Split(',');
MailMessage mailMessage = new MailMessage();
foreach (var email from emails)
{
     var emailAddressAttribute = new EmailAddressAttribute();
     if(emailAddressAttribute.IsValid(email))
     {
          MailMessage.To.Add(email);
     }
}
Mellisamellisent answered 12/9, 2023 at 19:54 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Dolce

© 2022 - 2024 — McMap. All rights reserved.