【问题标题】:What is the value returned by mailItem.Recipients[i].Address represent?mailItem.Recipients[i].Address 返回的值代表什么?
【发布时间】:2020-08-05 10:45:01
【问题描述】:
根据MicrosoftmailItem.Recipients[i].Address 返回如下内容:
/O=MFC2013/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=B370134F8FFD4CF3A0023F27B6B61F7D-ADMINISTRATOR
这些值是什么以及它们是如何以及在哪里配置的?
【问题讨论】:
标签:
vba
outlook
outlook-addin
【解决方案1】:
这是一个“EX”类型的完全有效的地址。如果需要 SMTP 类型的地址,则需要使用Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress(准备处理空值和异常)。
要优化 SMTP 地址检索,你的策略必须是
确定收件人地址类型。未通过 OOM 公开,但您可以从收件人表中检索 PR_ADDRTYPE 属性(您可以在 OutlookSpy 中看到它 - 单击 IMessage 按钮,转到 GetRecipientTable 选项卡)。使用 DASL 属性名称 ("http://schemas.microsoft.com/mapi/proptag/0x3002001F") 来读取使用 Recipient.PropertyAccessor.GetProperty() 的属性。
如果不是“EX”,只需阅读Recipient.Address 属性
对于“EX”地址类型,尝试使用Recipient.PropertyAccessor.GetProperty() 读取PR_SMTP_ADDRESS 属性(DASL 名称"http://schemas.microsoft.com/mapi/proptag/0x39FE001F")。如果缺少该属性,则会引发异常(您需要处理它)。
如果上面的#3 失败,作为最后的手段,尝试使用Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress。准备好处理空值和异常,例如,存在网络连接问题,或者如果在没有 Exchange Server 或使用来自不同组织的 Exchange Server 的情况下访问配置文件。
【解决方案2】:
Microsoft Exchange Server 可以操作电子邮件地址类型,例如 Exchange、SMTP、X.400、Microsoft Mail 等。默认情况下,Mail.Recipient 对象的 Address 属性仅返回 Exchange 类型地址,例如,这个:
/O=MFC2013/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=B370134F8FFD4CF3A0023F27B6B61F7D-ADMINISTRATOR
要检索 SMTP 地址,请使用以下调用序列:
Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress
理论上,要获取其他地址类型,我们需要通过IAddrBook.ResolveName方法在Outlook通讯录中找到收件人,然后通过IAddrBook.OpenEntry方法到达IMailUser接口并获取PR_EMS_AB_PROXY_ADDRESSES属性.
Function ResolveDisplayNameToSMTP(sFromName)
Dim oRecip As Outlook.Recipient
Dim oEU As Outlook.ExchangeUser
Dim oEDL As Outlook.ExchangeDistributionList
Set oRecip = Application.Session.CreateRecipient(sFromName)
oRecip.Resolve
If oRecip.Resolved Then
Select Case oRecip.AddressEntry.AddressEntryUserType
Case OlAddressEntryUserType.olExchangeUserAddressEntry
Set oEU = oRecip.AddressEntry.GetExchangeUser
If Not (oEU Is Nothing) Then
ResolveDisplayNameToSMTP = oEU.PrimarySmtpAddress + vbCrLf + oEU.BusinessTelephoneNumber
End If
Case OlAddressEntryUserType.olOutlookContactAddressEntry
Set oEU = oRecip.AddressEntry.GetExchangeUser
If Not (oEU Is Nothing) Then
ResolveDisplayNameToSMTP = oEU.PrimarySmtpAddress + vbCrLf + oEU.BusinessTelephoneNumber
End If
Case OlAddressEntryUserType.olExchangeDistributionListAddressEntry
Set oEDL = oRecip.AddressEntry.GetExchangeDistributionList
If Not (oEDL Is Nothing) Then
ResolveDisplayNameToSMTP = oEU.PrimarySmtpAddress + vbCrLf + oEU.BusinessTelephoneNumber
End If
End Select
End If
End Function ' ResolveDisplayNameToSMTP