Delphi:通过 Outlook 发送带有多个附件的电子邮件

标签 delphi delphi-xe2 delphi-2010 delphi-xe

各位专家大家好,

procedure TForm1.domail(Sender: TObject; fromname, fromadd, sub, toadd, thedocdone, theacc: string; body: widestring);
const
  olMailItem = 0;
var
  Outlook: OLEVariant;
  vmailitem: variant;
  Attachment: TIdAttachment;
  savetofol: string;
begin
  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
  vmailitem := Outlook.CreateItem(olMailItem);
  vmailitem.Recipients.Add(toadd);
  vmailitem.ReplyRecipients.Add('email@email.com');
  vmailitem.Subject := sub;
  vmailitem.body := 'SENT: ' + formatdatetime('dd mmmm yyyy - hh:nn am/pm', now) + #13#10 + body;
  vmailitem.ReadReceiptRequested := true;
  vmailitem.importance := 2;
  if thedocdone <> 'NIL' then
  begin
    vmailitem.Attachments.Add(thedocdone, 1, 1, 'SBSA_' + theacc);
    if ansipos('string1', lowercase(toadd)) <> 0 then
    begin
      vmailitem.Attachments.Add('*manual path', 1, 2, '*manual name');
      Memo1.Lines.Add('Adding consent letter to mail...');
    end;
    if ansipos('string2', lowercase(toadd)) <> 0 then
    begin
      vmailitem.Attachments.Add('*manual path', 1, 2, '*manual name');
      Memo1.Lines.Add('Adding consent letter to mail...');
    end;
    savetofol := extractfilepath(thedocdone) + copy(extractfilename(thedocdone), 0, length(extractfilename(thedocdone)) - 8);
    vmailitem.saveas(savetofol + '_eml.doc', 4); // ^ +'.doc'
  end;
  // vmailitem.clear;
  vmailitem.Send;
  Outlook := Unassigned;
end;

通过上面的代码,我可以附加到 Outlook 并发送电子邮件并将附件附加到该邮件...

我的问题是它不会附加第二个附件...???我已尝试使用不同的方法来执行此操作,但我只是无法将第二个附件附加到邮件中...

请帮忙...

最佳答案

参见Attachments Object (Outlook) :

To ensure consistent results, always save an item before adding or removing objects in the Attachments collection of the item.

错误:

vmailitem.Attachments.Add();
vmailitem.Attachments.Add();
vmailitem.Attachments.Add();

右:

vmailitem.Attachments.Add();
vmailitem.save;
vmailitem.Attachments.Add();
vmailitem.save;
vmailitem.Attachments.Add();
vmailitem.save;

关于Delphi:通过 Outlook 发送带有多个附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34941775/

相关文章:

windows - 如何使用 Delphi 2010 以编程方式确定 Windows 的性能设置

delphi - 分发使用 ActiveX 控件的 Delphi 应用程序

delphi - 在 Delphi 中,TPropertyAttributes 集的 paVCL 标志对生成的属性编辑器表示什么

sockets - 通过TClientSocket从网站加载图像

delphi - Delphi 2010中变体的函数空值

delphi - 有没有办法在Delphi中仅跟踪项目源?

Delphi Soap 请求程序 - 访问 TLS1.1/TLS1.2 时出现问题

delphi - 如何设置 Excel 列类型和格式?

Delphi XE2 ListView 工件

delphi - 将 Canvas 另存为图像