gpt4 book ai didi

Delphi Indy 附件不工作

转载 作者:行者123 更新时间:2023-12-03 02:26:05 26 4
gpt4 key购买 nike

我有一个应用程序需要从网络驱动器进行一些 FTP 上传。我为此使用 Indy。然后,当文件位于网络驱动器上并成功上传到 FTP 服务器时,我想通过电子邮件将同一文件发送给同事。

我正在使用下面的代码来执行此操作。电子邮件发送得很好,但由于某种原因,附件从未发送。我的代码做错了什么?

我将文件(在 FTP 过程中)添加到名为 EmailFiles (TStringList) 的公共(public)(表单)成员变量中,并将其传递给过程。在这里,我获取该文件名列表并尝试将其添加到我的 TIdMessage 组件中。发送电子邮件时,没有附件......

    procedure TfrmMain.SendEmail(FromMail, ToMail, Subject, Body: String;
Attachments: TStringList);
var
i: Integer;
Att : TIdAttachmentFile;
begin
Memo1.Lines.Add('');
Memo1.Lines.Add('Starting Email service...');
SMTP.Host := 'mail.*****.com';
SMTP.Username := '***UN***';
SMTP.Password := '***PW***';
try
Msg1.From.Address := FromMail;
Msg1.Recipients.EmailAddresses := ToMail;
Msg1.Subject := Subject;
Msg1.Body.Add(Body);

//Add attachment(s)
if Attachments.Count <= 0 then Memo1.Lines.Add('Warning: Cannot detect attachments for the Email...');

for i := 0 to Attachments.Count - 1 do
begin
if FileExists(Attachments[i]) then
begin
//Memo1.Lines.Add('Adding Attachment ' + Msg1.MessageParts.Items[0].FileName + '...');
Att := TIdAttachment.Create(Msg1.MessageParts, Attachments[i]);
Msg1.MessageParts.Add; //an attempt to explicitly ADD the Att object, to no avail
Memo1.Lines.Append('Added Attachment ' + Attachments[i]);
Att.Free;
end
else
begin
Memo1.Lines.Add('Could not locate file: ' + Attachments[i] + ' for Email attachment!');
end;
end;

//Try to send the message
try
SMTP.Connect;
if Msg1.MessageParts.AttachmentCount > 0 then begin
SMTP.Send(Msg1);
Memo1.Lines.Add('Sent Email successfully!');
end
else begin
if Messagedlg('Do you want to send the Email without attachments?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
SMTP.Send(Msg1);
Memo1.Lines.Add('Sent Email successfully, without attachments!');
end
else
Memo1.Lines.Add('No files attached to the Email Message - Cannot send!');
end;

except
on E:Exception do
begin
Messagedlg('Could not send the Email Message!' + #13#10 + E.Message, mtError, [mbOK], 0);
end;
end;

except
on E:Exception do
ShowMessage('Could not connect to SMTP Server' + #13#10 + E.Message);
end;
end;

最佳答案

不要释放 Att 对象,调用 Msg1.MessageParts.Add 不会执行任何操作。

if FileExists(Attachments[i]) then
begin
TIdAttachment.Create(Msg1.MessageParts, Attachments[i]);
end
else
begin
Memo1.Lines.Add('Could not locate file: ' + Attachments[i] + ' for Email attachment!');
end;

您还需要指定电子邮件内容类型:

Msg1.ContentType := 'multipart/mixed';

请引用此Indy Blog ,请参阅“HTML 和不相关附件以及无纯文本”部分

关于Delphi Indy 附件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428911/

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