我们使用Delphi7运行的软件生成报告,并通过电子邮件将报告发送给各个利益相关者。在每天传输的大约30-40个报告中,每天有2-4个不同的报告由于异常而失败:"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:连接正常关闭”
有什么建议吗?
发布于 2013-06-06 00:42:42
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;https://stackoverflow.com/questions/16944708
复制相似问题