【发布时间】:2017-08-25 13:56:37
【问题描述】:
我有一种方法可以更新表中的行。它遍历受更改影响的所有公司客户,并通过电子邮件向所有应通知此更改的所有用户发送电子邮件,并附上可能的 pdf 文件。该文件保存在服务器上的特定位置。 这最终可能会从该方法调用发送 20 到 500 封电子邮件。
此方法由客户端调用一次,然后服务器完成所有工作。用于电子邮件的 PDF 文件永远不会更改,始终保持不变。
也许这种方法有更好的结构,或者我可以采取一些保护措施来避免服务器在运行该脚本时耗尽资源?
截至目前,出现以下错误:An existing connection was forcibly closed by the remote host
System.Net.SocketsException: An existing connection was forcibly closed by the remote host 这是客服发给我的堆栈跟踪。
这里是该方法的大部分代码:
string attach = null;
string mergerFile = FileHelper.DirectoryName.MergerDocumentDirectory(instId) + mergerDocumentFileName;
if (System.IO.File.Exists(mergerFile))
attach = mergerFile;
string subject = "subject";
StringBuilder sbEmailBody = new StringBuilder();
sbEmailBody.Append("the email body etc...");
foreach(Company in companies){
//do some business logic here
foreach(User in Company.Users){
CustomLibrary.Mail.SendEmail("from@mail.com", user.Email.Trim(), "", "", subject, true, emailBody, attach);
}
}
CustomLibrary.Mail.SendEmail 方法
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
smtpClient.Host = WebConfigurationManager.AppSettings["mailServer"];
smtpClient.Port = 25;
message.From = new MailAddress(EmailFrom);
message.To.Add(EmailTo);
message.Subject = EmailSubject;
#region mail body
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//add some header, body and footer to the email body here
#endregion
// Message body content
message.Body = sb.ToString();
//Attachment
message.Attachments.Clear();
if (!String.IsNullOrEmpty(AttachmentFile))
{
Attachment myattach = new Attachment(AttachmentFile);
message.Attachments.Add(myattach);
}
// Send SMTP mail
smtpClient.Send(message);
【问题讨论】:
-
@rogue1nib 我读了这篇文章。在我看来这是一种不同的情况
-
我能解释一下我如何解决我的问题吗?我没有从那篇文章中读到任何对我有帮助的内容