gpt4 book ai didi

delphi - 对 EIdConnClosedGraceively 异常进行故障排除?

转载 作者:行者123 更新时间:2023-12-03 15:25:22 25 4
gpt4 key购买 nike

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

我试图追踪为什么会发生这种情况以及如何在代码中捕获它。这是我们目前所拥有的:

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;

此外,当触发 EIdConnClosedGraceously 时,它始终显示“尝试#2:连接正常关闭”。并且永远不要“尝试#1:连接正常关闭”

有什么建议吗?

最佳答案

EIdConnClosedGraceively 表示对方(本例中为 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;

关于delphi - 对 EIdConnClosedGraceively 异常进行故障排除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16944708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com