2017-08-24 149 views
-1

我有一种方法可以更新表中的行。它贯穿所有受变更影响的公司,并且每个公司都会通过电子邮件向所有应该通知此变更的用户发送附带的可能的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以下是客户服务寄给我的堆栈跟踪。 enter image description here

这里是最该方法的代码:

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); 
+0

可能重复[随机“现有连接被远程主机强行关闭”。在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

+0

@ rogue1nib我读了帖子。在我看来,这是一种不同的情况 – Greg

+0

我可以解释我如何解决我的问题?我不会从那篇文章中读到任何帮助我的文章 – Greg

回答

0

您应该检查您的邮件服务器的日志。你可能超过了服务器设置的一些限制,这会导致它拒绝你的连接。

如果您无法控制邮件服务器,我会建议您设置一个本地SMTP服务器(例如,IIS SMTP服务非常简单并且可用于此功能)。您的代码假脱机在本地SMTP服务中的电子邮件,然后可以做适当的重试,超时等。

+0

我认为这是一个开始感谢你的好地方。我会看看 :) – Greg

相关问题