指定的字符串不是电子邮件地址所需的格式。发送电子邮件时

The specified string is not in the form required for an e-mail address. when sending an email

尝试使用以下代码向多个收件人发送电子邮件时出现此错误:

The specified string is not in the form required for an e-mail address.

string[] email = {"emailone@gmail.com","emailtow@gmail.com"};
using (MailMessage mm = new MailMessage("teset123321@gmail.com", email.ToString()))
{
     try
     {
          mm.Subject = "sub;
          mm.Body = "msg";
          mm.Body += GetGridviewData(GridView1);
          mm.IsBodyHtml = true;
          SmtpClient smtp = new SmtpClient();
          smtp.Host = "smtpout.server.net";
          smtp.EnableSsl = false;
          NetworkCredential NetworkCred = new NetworkCredential("email", "pass");
          smtp.UseDefaultCredentials = true;
          smtp.Credentials = NetworkCred;
          smtp.Port = 80;
          smtp.Send(mm);
          ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
      }
      catch (Exception ex)
      {
          Response.Write("Could not send the e-mail - error: " + ex.Message);    
      }
}

目前您的代码:

new NetworkCredential("email", "pass");

"email" 视为最终不是电子邮件地址的电子邮件地址,而是包含电子邮件地址的字符串数组。

所以你可以这样试试:

foreach (string add in email)
{
    mm.To.Add(new MailAddress(add));
}

将您的使用行更改为:

using (MailMessage mm = new MailMessage())

添加发件人地址:

mm.From = new MailAddress("myaddress@gmail.com");

您可以遍历电子邮件地址的字符串数组,然后像这样将它们一一添加:

string[] email = { "emailone@gmail.com", "emailtwo@gmail.com", "emailthree@gmail.com" };

foreach (string address in email)
{
    mm.To.Add(address);
}

示例:

string[] email = { "emailone@gmail.com", "emailtwo@gmail.com", "emailthree@gmail.com" };

using (MailMessage mm = new MailMessage())
{
    try
    {
        mm.From = new MailAddress("myaddress@gmail.com");

        foreach (string address in email)
        {
            mm.To.Add(address);
        }

        mm.Subject = "sub;
        // Other Properties Here
    }
   catch (Exception ex)
   {
       Response.Write("Could not send the e-mail - error: " + ex.Message);
   }
}

我在处理 SMTP.js 时遇到了这个问题,但问题来自空白,所以请尝试 trim 在处理电子邮件时将其关闭。我希望这对某人有所帮助。