gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:27:33 25 4
gpt4 key购买 nike

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

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);
}
}

最佳答案

将您的使用行更改为:

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);
}
}

关于c# - 指定的字符串不是电子邮件地址所需的格式。发送电子邮件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32331622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com