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

时间:2016-01-22 08:03:02

标签: 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并发送电子邮件并附上该邮件的附件......

我的问题是IT WONT附上第二个附件...... ???我已尝试使用不同的方法做到这一点,但我只是不能得到附加到邮件的第二个附件......

请帮忙......

1 个答案:

答案 0 :(得分:2)

请参阅Attachments Object (Outlook)

  

为确保结果一致,请始终在添加或之前保存项目   删除项目的“附件”集合中的对象。

错:

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

右:

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