我有一种方法可以更新表中的行。它贯穿所有受变更影响的公司,并且每个公司都会通过电子邮件向所有应该通知此变更的用户发送附带的可能的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("[email protected]", 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);
可能重复[随机“现有连接被远程主机强行关闭”。在TCP重置后](https://stackoverflow.com/questions/32502207/random-an-existing-connection-was-forcibly-closed-by-the-remote-host-after-a) – 2017-08-24 15:52:57
@ rogue1nib我读了帖子。在我看来,这是一种不同的情况 – Greg
我可以解释我如何解决我的问题?我不会从那篇文章中读到任何帮助我的文章 – Greg