试图使用下面的代码发送电子邮件给多个收件人,我得到了这个错误。
指定的字符串不是电子邮件地址所需的形式。
string[] email = {"[email protected]","[email protected]"};
using (MailMessage mm = new MailMessage("[email protected]", 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);
}
}
把你的使用行改成这样。
using (MailMessage mm = new MailMessage())
添加from地址
mm.From = new MailAddress("[email protected]");
你可以循环浏览你的电子邮件地址字符串数组,然后像这样一个一个地添加它们。
string[] email = { "[email protected]", "[email protected]", "[email protected]" };
foreach (string address in email)
{
mm.To.Add(address);
}
例如:
string[] email = { "[email protected]", "[email protected]", "[email protected]" };
using (MailMessage mm = new MailMessage())
{
try
{
mm.From = new MailAddress("[email protected]");
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);
}
}
目前你的代码:
new NetworkCredential("email", "pass");
treats "email"
作为一个电子邮件地址,而这个地址最终不是一个电子邮件地址,而是一个包含电子邮件地址的字符串数组。
所以你可以这样尝试。
foreach (string add in email)
{
mm.To.Add(new MailAddress(add));
}
我在使用SMTP.js的时候也遇到了这个问题 但问题来自于空白处 所以在处理邮件的时候请尝试把空白处剪掉。希望能帮到大家。