我使用代码发送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();
}
PopulateBody功能:
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.Net.Mail.SmtpException”类型的未处理异常 发生在System.dll中附加信息:服务不可用, 关闭传输通道。服务器响应是:4.7.0临时 系统问题。稍后再试(WS)。 u12sm5363862pfg.146 - gsmtp
不知道什么是错的。任何有关这方面的帮助将不胜感激。
Plz不要标记它重复或任何东西,因为我搜索了很多解决方案,但没有找到正确的。谢谢提前!!
答案 0 :(得分:0)
我在我的项目中使用它。它的工作性很好。发送10-12封或更多邮件后,不会出现任何错误。
public static void Send(string from, string to, string cc, string subject, string message, string[] attachment, byte[] fileByte, string fileByteName)
{
string ccChar;
if (!string.IsNullOrEmpty(cc) && cc.EndsWith(","))
ccChar = cc.Remove(cc.Length - 1, 1);
else
ccChar = cc;
if (smtp_address == "")
{
smtp_address = ConfigurationManager.AppSettings["smtp_address"];
smtp_port = int.Parse(ConfigurationManager.AppSettings["smtp_port"]);
smtp_user = ConfigurationManager.AppSettings["smtp_user"];
smtp_password = ConfigurationManager.AppSettings["smtp_password"];
smtp_ssl = bool.Parse(ConfigurationManager.AppSettings["smtp_ssl"]);
}
MailMessage mm = new MailMessage(from, to, subject, message);
mm.Bcc.Add(ConfigurationManager.AppSettings["mailAlertMonitor"]);
mm.IsBodyHtml = true;
if (!string.IsNullOrEmpty(ccChar)) mm.CC.Add(ccChar);
if (fileByte.Length != 0 && !string.IsNullOrEmpty(fileByteName))
{
mm.Attachments.Add(new Attachment(new MemoryStream(fileByte), fileByteName));
}
if (attachment != null)
{
foreach (string file in attachment)
{
if (!string.IsNullOrEmpty(file))
{
Attachment attc = new Attachment(file);
mm.Attachments.Add(attc);
}
}
}
SmtpClient smtp = new SmtpClient(smtp_address, smtp_port);
smtp.Credentials = new NetworkCredential(smtp_user, smtp_password);
smtp.EnableSsl = smtp_ssl;
smtp.Send(mm);
InsertSendEmailLog(from, to, cc, ConfigurationManager.AppSettings["mailAlertMonitor"], subject, message);
}
我希望它可以帮到你。
答案 1 :(得分:0)
尝试这样,让我知道。
private static string smtp_address = "";
private static int smtp_port = 587;
private static string smtp_user = "";
private static string smtp_password = "";
private static bool smtp_ssl = false;
private bool SendMassEmail(string from, string to, string cc, string subject, string message, string body)
{
smtp_address = WebConfigurationManager.AppSettings["Host"];
smtp_port = int.Parse(WebConfigurationManager.AppSettings["Port"]);
smtp_user = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["UserName"];
smtp_password = WebConfigurationManager.AppSettings["Password"]);
smtp_ssl = smtp_ssl;
MailMessage mm = new MailMessage(from, to, subject, message);
SmtpClient smtp = new SmtpClient(smtp_address, smtp_port);
smtp.Credentials = new NetworkCredential(smtp_user, smtp_password);
smtp.EnableSsl = smtp_ssl;
smtp.Send(mm);
return true;
}