我正在使用代码发送 SMTP 电子邮件:
protected void btnSendEmailToAll_Click(object sender, EventArgs e)
{
string username = Master.User;
//Create a temporary DataTable
DataTable dtCustomers = new DataTable();
dtCustomers.Columns.AddRange(new DataColumn[3] { new DataColumn("name", typeof(string)),
new DataColumn("email",typeof(string)), new DataColumn("EmailSentOn",typeof(string)) });
//Copy the Checked Rows to DataTable
foreach (GridViewRow row in Grid.Rows)
{
// Only look in data rows, ignore header and footer rows
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkrow");
if (ChkBoxRows.Checked == true)
{
string Name = (row.FindControl("lblname") as Label).Text.ToLower();
dtCustomers.Rows.Add(Name, (row.FindControl("lblemail") as Label).Text, (row.FindControl("lblEmailsentOn") as Label).Text);
var id = Grid.DataKeys[row.RowIndex].Value;
using (var db = new DatabaseHelper())
{
db.ExecNonQuery(Queries.UpdateReminderEmailSentData, "@RW", id);
}
}
}
}
string subject = "My Subject";
//Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtCustomers.AsEnumerable(), row =>
{
string body = this.PopulateBody(row["name"].ToString(), row["EmailSentOn"].ToString(), username);
SendMassEmail(row["email"].ToString(), subject, body.ToString());
});
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Email Sent Successfully')", true);
BindGrid();
}
填充体功能:
private string PopulateBody(string name, string date, string username)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/followupemailtemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{Name}", name);
body = body.Replace("{Date}", date);
body = body.Replace("{UserName}", username);
return body;
}
SendMassEmail 功能:
private bool SendMassEmail(string recipient, string subject, string body)
{
var smtp = new SmtpClient()
{
Host = WebConfigurationManager.AppSettings["Host"],//smtp.gmail.com
EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableSsl"]),//true
UseDefaultCredentials = true,
Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["UserName"], WebConfigurationManager.AppSettings["Password"]),
Port = int.Parse(WebConfigurationManager.AppSettings["Port"])//587
};
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(WebConfigurationManager.AppSettings["UserName"], "My Email");
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recipient));
smtp.Send(mailMessage);
}
return true;
}
发送五封电子邮件需要 30 秒:(
我尝试使用类似的线程(即使我被建议如果我们使用parallel.foreach就不需要使用线程):
Thread T1 = new Thread(delegate ()
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(WebConfigurationManager.AppSettings["UserName"], "My Email");
mailMessage.Subject = subject;
mailMessage.Body = ShowBody;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
smtp.Send(mailMessage);
}
});
T1.Start();
这很快,但不会发送所有未发送的电子邮件。
即在发送 10-12 封电子邮件后,有时甚至更少,它会抛出异常:
System.dll 中发生“System.Net.Mail.SmtpException”类型的未处理异常附加信息:服务不可用,正在关闭传输通道。服务器响应为:4.7.0 临时系统问题。稍后再试 (WS)。u12sm5363862pfg.146 - gsmtp
不知道怎么回事。对此的任何帮助将不胜感激。
请不要将其标记为重复或任何内容,因为我搜索了很多解决方案但找不到正确的解决方案。提前致谢!