Asp.Net MVC 电子邮件:在邮件中发现无效字符 header

Asp.Net MVC Email: An invalid character was found in the mail header

我开发了一个允许向多个收件人发送电子邮件的应用程序。为了从收件人那里获取电子邮件,我使用了自动完成功能并用“,”分隔各种电子邮件示例:aaa @ gmail.com、bbb @ gmail.com

问题是点击发送时,它不起作用,您收到以下错误:邮件中发现无效字符 header: ','.

控制器

[HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(EmailModel model, List<HttpPostedFileBase> attachments)
        {
            model.Email = "xxxxx@gmail.com";
            using (MailMessage mm = new MailMessage(model.Email, model.Destinatário))
            {
                mm.From = new MailAddress("xxxxx@gmail.com");
                model.Password = "xxxxx";
                mm.Subject = model.Assunto;
                mm.Body = model.Mensagem;

                foreach (HttpPostedFileBase attachment in attachments)
                {
                    if (attachment != null)
                    {
                        string fileName = Path.GetFileName(attachment.FileName);
                        mm.Attachments.Add(new Attachment(attachment.InputStream, fileName));
                    }
                }
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
                ViewBag.Message = "Sucess!";
            }
            return View();
        }

JavaScript

 <script type="text/javascript">
        $(function () {

            $("#email").autocomplete({
                source: function (request, response) {
                    $.ajax(
                        {
                            url: '/Email/AutoComplete/',
                            data: "{ 'prefix': '" + GetCurrentSearchTerm(request.term) + "'}",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            cache: false,
                            success: function (data) {
                                response($.map(data, function (item) {
                                    return {
                                        label: item.label,
                                        value: item.val,
                                        nome: item.val
                                    };
                                }))
                            }
                        })
                },
                select: function (event, ui) {
                    var LastValue = splitCurrentText(this.value);
                    LastValue.pop();
                    LastValue.push(ui.item.value);
                    LastValue.push("");
                    this.value = LastValue.join(",");
                    return false;
                },
                focus: function () {
                    return false;
                }
            });
            function splitCurrentText(LastTerm) {

                return LastTerm.split(/,\s*/);
            }

            function GetCurrentSearchTerm(LastTerm) {

                return splitCurrentText(LastTerm).pop();
            }
        });
    </script>

MailMessage class 不支持逗号分隔地址。相反,将每个地址单独添加到 To 成员,就像这样。

using (MailMessage mm = new MailMessage())
{
    var toAddresses = model.Destinatário.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries);

    foreach (var toAddress in toAddresses)
    {
        mm.To.Add(new MailAddress(toAddress));
    }

    mm.From = new MailAddress("xxxxx@gmail.com");
    model.Password = "xxxxx";
    mm.Subject = model.Assunto;
    mm.Body = model.Mensagem;
    ...
}

另请参阅:How to send Email to multiple Recipients with MailMessage?