首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排除EIdConnClosedGracefully异常故障?

排除EIdConnClosedGracefully异常故障?
EN

Stack Overflow用户
提问于 2013-06-06 00:05:11
回答 1查看 2.8K关注 0票数 4

我们使用Delphi7运行的软件生成报告,并通过电子邮件将报告发送给各个利益相关者。在每天传输的大约30-40个报告中,每天有2-4个不同的报告由于异常而失败:"EIdConnClosedGracefully“

我正在尝试追踪为什么会发生这种情况,以及如何在代码中捕捉到这种情况。以下是我们目前所掌握的信息:

代码语言:javascript
复制
try
   // Code that Populates "mess" (a tIdMessage variable)

   if (not nSMTP.Connected) then
   begin
     nSMTP.Connect;
   end;

   try
     nSMTP.Send(mess);      
   except on E : Exception do
     begin
       resend := E.Message;
       // AutoReports_ExceptionMessage is a string that holds the Exception message
       AutoReports_ExceptionMessage := 'Attempt #1: ' + resend; 
     end;
   end;

   if (resend <> '') then   // If there is an exception triggered, resend the email
   begin
     try
       nSMTP.Send(mess);
     except on E : Exception do
       begin
         resend := E.Message;
         AutoReports_ExceptionMessage := 'Attempt #2: ' + resend;  
       end;
     end;
   end

finally
  mess.Free;
end;

此外,当EIdConnClosedGracefully被触发时,它总是显示“尝试#2:连接正常关闭”。永远不要“尝试#1:连接正常关闭”

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-06 00:42:42

EIdConnClosedGracefully表示另一方(在本例中为SMTP服务器)已断开其连接的一端。一旦引发了该异常,您就不能向另一方发送更多数据。您必须先重新连接。因此,在您显示的代码中,如果尝试#1由于套接字断开连接而失败,则尝试#2将始终失败。

试着这样做:

代码语言:javascript
复制
for Attempt := 1 to 2 do
begin
  try
    //...
    if (not nSMTP.Connected) then
      nSMTP.Connect;
    nSMTP.Send(mess);
    AutoReports_ExceptionMessage := '';
    Break;
  except
    on E : Exception do
    begin
      AutoReports_ExceptionMessage := E.Message; 
      nSMTP.Disconnect;
    end;
  end;
end; 

或者:

代码语言:javascript
复制
try
  // ...

  if (not nSMTP.Connected) then
     nSMTP.Connect;

  try
    nSMTP.Send(mess);      
    AutoReports_ExceptionMessage := ''; 
  except
    on E : Exception do
    begin
      AutoReports_ExceptionMessage := E.Message; 
      nSMTP.Disconnect;      
      try
        nSMTP.Connect;      
        nSMTP.Send(mess);
        AutoReports_ExceptionMessage := ''; 
      except
        on E : Exception do
        begin
          AutoReports_ExceptionMessage := E.Message;
          nSMTP.Disconnect;      
        end;
      end;
    end;
  end;
end;
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16944708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档