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
相关产品推荐
相关产品推荐

