没有从Outlook获得正确的返回值

时间:2015-07-05 17:42:59

标签: delphi email outlook

我使用以下代码使用Outlook 2010发送电子邮件,但行

Result := MailItem.Sent;

返回错误

  

该项目已被移动或删除

如何解决此问题,以便函数为我提供可用的返回值?

function olSendMail(aFrom, aSubject, aBody, aTo, aCC, aBcc: string; aMailFiles: TStringList; aReceipt: boolean = False; aPreview: boolean = True): boolean;
var
  Outlook: OleVariant;
  MailItem: OleVariant;
  i: integer;
  MailInspector: variant;
const
  olMailItem = $00000000;
begin
  try
    try
      Outlook := GetActiveOleObject('Outlook.Application');
    except
      Outlook := CreateOleObject('Outlook.Application');
    end;
    MailItem := Outlook.CreateItem(olMailItem);
    if olAccountValid(aFrom) then
      MailItem.SendUsingAccount := Outlook.Session.Accounts.Item(aFrom);
    MailItem.To := aTo;
    MailItem.Cc := aCc;
    MailItem.Bcc := aBcc;
    MailItem.Subject := aSubject;
    MailItem.Body := aBody;
    for i := 0 to aMailFiles.Count - 1 do
      MailItem.Attachments.Add(aMailFiles.Strings[i]);
    MailItem.ReadReceiptRequested := aReceipt;
    MailItem.OriginatorDeliveryReportRequested := aReceipt;

    if aPreview = True then
      begin
        MailInspector := MailItem.GetInspector;
        MailInspector.Display(True);
      end
    else
      MailItem.Send;
    Result := MailItem.Sent;
  except
    on E:Exception do
      begin
        Logfile.Error('U_Mailing.Outlook.SendMailOutlook: ' + E.Message);
        Result := False;
      end;
  end;
end;

1 个答案:

答案 0 :(得分:1)

调用Sent后,唯一允许的操作是释放对MailItem对象的引用 - 它现在属于假脱机程序,并且将异步发送,然后移动到已发送邮件文件夹。仅在此时(现在将是不同的消息),Sent属性将为true,并且将填充所有与发件人相关的属性。