简体   繁体   English

MailMessage.To.Add() 抛出异常:“在邮件标题中发现无效字符:','。”

[英]MailMessage.To.Add() throwing exception : “An invalid character was found in the mail header: ','.”

I am getting this error when I am using it in sharepoint project, while in console app its working fine当我在 sharepoint 项目中使用它时出现此错误,而在控制台应用程序中它工作正常

I am using MailMessage class to send email using SMTP .我正在使用MailMessage类使用 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 .但是,当我尝试将用户添加到 'To' 属性时,我得到 {"An invalid character was found in the mail header: ','."} 异常,我认为这里发生了一些可疑的事情,因为 ',' 被允许分隔多个用户。 Adding multiple user添加多个用户

** Multiple e-mail addresses must be separated with a comma character (",").** ** 多个电子邮件地址必须用逗号字符 (",") 分隔。**

MailMessage mailMessage = new MailMessage();

 mailMessage.To.Add("amir_khan@enter.com,walter_white@yahoo.com,");

Got the culprit: It's the extra comma( , ) at the end of last email address罪魁祸首:是最后一个电子邮件地址末尾的额外逗号(

mailMessage.To.Add("amir_khan@enter.com,walter_white@yahoo.com,");

Just removed that and voila!刚刚删除它,瞧! its working.它的工作。 Don't know why it's working in console application but not in sharepoint :(不知道为什么它可以在控制台应用程序中运行,但不能在 sharepoint 中运行 :(

mailMessage.To.Add("amir_khan@enter.com,walter_white@yahoo.com");

If this does not work in SharePoint then please add each address separately onto MailMessage object like below;如果这在 SharePoint 中不起作用,那么请将每个地址分别添加到 MailMessage 对象中,如下所示;

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

} }

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)我必须将我的代码从 string.Join(",", emailList) 更改为 string.Join(", ", emailList)

Following didn't work for me.以下对我不起作用。

mailMessage.To.Add("amir_khan@enter.com,walter_white@yahoo.com");

Following worked for me(Observe that there is space after the comma).以下对我有用(注意逗号后面有空格)。

mailMessage.To.Add("amir_khan@enter.com, walter_white@yahoo.com");

I can't replicate this.我无法复制这一点。 The above code works for me.上面的代码对我有用。 Maybe try to add them using a seperate 'To' each time.也许尝试每次使用单独的“To”添加它们。

mailMessage.To.Add(x);
mailMessage.To.Add(y);

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 = myname@mycompany.com   

got added被添加

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

Essentially:本质上:

myname@mycompany.com@mycompany.com    

So instead of an issue with a trailing comma, literally another @因此,不是尾随逗号的问题,而是另一个@

In my case I forgot to filter out users without email, so I was forming my list of emails like this:就我而言,我忘记过滤掉没有电子邮件的用户,所以我形成了这样的电子邮件列表:

username1@ldomain.com
username2@domain.com
@domain.com  //PROBLEM HERE
username3@linamar.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM