You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi通过Outlook OLE指定发送账户失效问题求助

Outlook多账户下指定发送账户失效问题解决

我需要通过Outlook从特定账户发送邮件,用户的Outlook应用中存在两个及以上账户,自动化流程需要指定特定账户进行邮件发送。根据OLE文档说明使用MailItem.SendUsingAccount属性后,生成的邮件显示时,FROM字段仍使用当前用户账户,而非通过SendUsingAccount设置的账户。

初始代码

procedure TOutlookOLE.AssignSenderFromAccounts(const _MailFromEmail: string);
var
  i: integer;
  NewSender: OleVariant;
  sendermail: string;

  report: string;
begin
  if Connected then
  begin
    i := OutlookAppObject.Session.Accounts.Count;

    // 遍历所有账户找到匹配项
    for I := 1 to OutlookAppObject.Session.Accounts.Count do
    begin

      NewSender  := OutlookAppObject.Session.Accounts.Item(i);
      sendermail := VarToStr(NewSender.SmtpAddress);

      // 通过邮箱地址匹配账户
      if NOT VarIsNull(NewSender) AND SameText(sendermail, _MailFromEmail) then
      begin

        // 找到匹配项并设置到邮件对象...
        // ...但显示时FROM字段并未改变
        if MailCreated then
              OutlookMailItem.SendUsingAccount := NewSender
        else  raise Exception.Create('Outlook OLE Exception: Mail object not created.');
        break;
      end;
    end;
  end
  else raise Exception.Create('Outlook OLE Exception: Outlook not connected.');
end;

修复后可行代码

经Dmitri回复后,更新后的方法可以正常生效:

procedure TOutlookOLE.AssignSenderFromAccounts(const _MailFromEmail: string);

  const PR_SENT_REPRESENTING_EMAIL_ADDRESS = 'http://schemas.microsoft.com/mapi/proptag/0x0065001F';

var
  i: integer;
  NewSender: OleVariant;
  sendermail: string;

  report: string;
begin
  if Connected then
  begin
    i := OutlookAppObject.Session.Accounts.Count;

    for I := 1 to OutlookAppObject.Session.Accounts.Count do
    begin
      NewSender  := OutlookAppObject.Session.Accounts.Item(i);
      sendermail := VarToStr(NewSender.SmtpAddress);

      if NOT VarIsNull(NewSender) AND SameText(sendermail, _MailFromEmail) then
      begin
        if MailCreated then
        begin
          OutlookMailItem.SendUsingAccount := NewSender;

          OutlookMailItem.PropertyAccessor.SetProperty(
            PR_SENT_REPRESENTING_EMAIL_ADDRESS,
            WideString(_MailFromEmail));
        end
        else  raise Exception.Create('Outlook OLE Exception: Mail object not created.');

        break;
      end;
    end;
  end
  else raise Exception.Create('Outlook OLE Exception: Outlook not connected.');
end;

内容的提问来源于stack exchange,提问作者Andrej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 19:33:27