2013-06-05 241 views
2

我们使用Delphi 7运行的软件通过电子邮件生成并发送报告给各个利益相关者。在每天传输的大约30-40个报告中,每天2-4个不同的报告因为例外而失败:“EIdConnClosedGracefully”疑难解答EIdConnClosedGracefully异常?

我试图追踪为什么会发生这种情况,以及如何在代码中捕获此信息。下面是我们到目前为止有:

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:连接关闭优雅地”

任何建议?

+0

这是一个正常的异常...黑体书写的源代码。你必须忽略它。 –

回答

4

EIdConnClosedGracefully表示另一方(本例中为SMTP服务器)已断开连接的结束。一旦发生异常,您无法将更多数据发送给对方。您必须先重新连接。因此,在您展示的代码中,如果尝试#1由于套接字断开而失败,则尝试#2将始终失败。

试试这个:

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; 

或者:

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

谢谢@Remy!我实现了第一部分,没有'如果nSMTP.IOHandler <> nil然后nSMTP.IOHandler.InputBuffer.Clear;'并且会在每天运行一夜之后完成报告。 (我得到InputBuffer未定义,我想也许这是在Delphi 7实施后引入的;或者我不包括必要的东西) –

+0

如果InputBuffer没有定义,那么你必须使用Indy 9我给你的是Indy 10。在Indy 10中,如果分配了'IOHandler'并且其'InputBuffer'不为空,'Connect()'将引发异常。 Indy 9的情况并非如此。 –

+0

非常感谢你@Remy!代码的第一部分起作用了,并且自从我开始跟踪这些错误以来第一次(可能大约三天,尽管我们已经丢失了**个月**的报告),所有这些错误都已发送!非常感激! –