gpt4 book ai didi

delphi - 无法使用 Indy 发送电子邮件,程序卡住

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

当我尝试使用 indy 发送带有附件的电子邮件时,我的程序卡住了,我不知道为什么。这是我用于发送电子邮件的表单的完整代码。

unit Dok_sutisana;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase,
IdSMTP, StdCtrls, Buttons, ComCtrls, IdAttachmentFile;

type
TForm14 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
BitBtn1: TBitBtn;
FontDialog1: TFontDialog;
RichEdit1: TRichEdit;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
BitBtn2: TBitBtn;
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form14: TForm14;

implementation

uses Autentif, EDGA;

{$R *.dfm}

procedure TForm14.BitBtn1Click(Sender: TObject);
begin
if FontDialog1.Execute() then
RichEdit1.Font:=FontDialog1.Font;
end;

procedure TForm14.BitBtn2Click(Sender: TObject);
var s:string;
begin
form3.ADOTable1.Active:=true;
//setup SMTP
IdSMTP1.Host := form3.adotable1['smtp'];
IdSMTP1.Port := form3.adotable1['ports'];
IdSMTP1.Username:= '******@gmail.com';
IdSMTP1.Password:='******';

//setup mail message
IdMessage1.From.Address := form3.adotable1['e-pasts'];
IdMessage1.From.Name:= form3.adotable1['Vards']+' '+ form3.adotable1['Uzvards'];
IdMessage1.Recipients.EMailAddresses := edit1.Text;;

IdMessage1.Subject := edit2.Text;
IdMessage1.Body.Add(RichEdit1.Text + form3.ADOTable1['paraksts']);
s:= GetCurrentDir + form1.ADOTable1['Dok_adr'];
TIdAttachmentFile.Create(IdMessage1.MessageParts, s ) ;

//send mail

IdSMTP1.Connect() ;
IdSMTP1.Send(IdMessage1) ;
IdSMTP1.Disconnect;
IdMessage1.Free;
IdSMTP1.Free;

form3.ADOTable1.Active:=false;
Form14.Close;
end;

end.

我希望通过这个我可以得到一些帮助来解决我的问题。

最佳答案

Indy 使用阻塞操作,并且您在主 UI 线程的上下文中使用 Indy。因此,当 TIdSMTP 繁忙时,您的主线程将被阻止处理新消息,从而导致应用程序卡住,直到 TIdSMTP 完成。

为了避免卡住,您可以:

  1. (首选解决方案)将您的 TIdSMTP 代码移至单独的工作线程中。

  2. TIdAntiFreeze 组件拖放到您的表单上。这将允许主消息队列继续处理新消息,而其他 Indy 组件在主线程中运行。

为了更好地衡量,您还应该设置 TIdSMTP.ConnectTimeoutTIdSMTP.ReadTimeout 属性,以便 Connect()Send () 不要长时间阻塞。如果发生超时,将引发适当的异常以中止操作。

关于delphi - 无法使用 Indy 发送电子邮件,程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23753815/

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