【发布时间】:2016-05-25 06:00:40
【问题描述】:
我有一个需要从网络驱动器进行一些 FTP 上传的应用程序。我正在为此使用 Indy。然后,当一个文件位于网络驱动器上并成功上传到 FTP 服务器时,我想将同一文件通过电子邮件发送给同事。
我正在使用下面的代码来执行此操作。电子邮件可以正常发送,但由于某种原因,附件始终无法发送。我的代码做错了什么?
我将文件(在 FTP 过程中)添加到名为 EmailFiles (TStringList) 的公共(表单)成员变量中,并将其传递给过程。在这里,我获取文件名列表并尝试将其添加到我的 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;
【问题讨论】:
-
也许还值得一提,从 Indy 10 开始,以下代码对我不起作用:(由于 create 有 1 个集合参数) Att := TIdAttachmentFile.Create(Msg1.MessageParts, Attachments[i ]);对此的修复基本上是按照以下方式做一些事情: with TIdAttachmentFile.Create(Msg1.MessageParts) do begin FileName := Attachments[i];结束;
标签: delphi rad indy10 delphi-10-seattle