【问题标题】:Extract SMTP address from Outlook Nickname从 Outlook 昵称中提取 SMTP 地址
【发布时间】:2019-08-06 10:04:55
【问题描述】:

我想在发送到外部地址时显示一条消息。我使用各种堆栈溢出问题来创建下面的 VBA。我使用 Office 365。

我发现 Outlook 指定为 Outlook 昵称的收件人无法使用 SMTP 地址解析。

相反,Recipients.Item(i).Address 属性解析为类似

/o=NT5/ou=000000000000000000000000000000000/cn=122E0E7203FE4F448EC35B53EE8523A4

我无法从中提取 SMTP 地址。我需要检查此收件人是否为外部收件人。

我尝试使用 Recipients.Item(i).Name 属性(仅包括地址的第一部分在 @ 之前)并尝试使用 Session.CreateRecipient 解决此问题,但失败了。我也对Recipients.Item(i).Address 属性进行了同样的尝试。

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim xMailItem As Outlook.MailItem
Dim xRecipients As Outlook.Recipients
Dim i As Long
Dim xRecipientAddress As String
Dim xExternal As Integer

On Error Resume Next

xExternal = 0

If Item.Class <> olMail Then
    Exit Sub
End If

Set xMailItem = Item
Set xRecipients = xMailItem.Recipients

For i = xRecipients.Count To 1 Step -1
    xRecipientAddress = xRecipients.Item(i).Address
   
    If Left(xRecipientAddress, 1) <> "/" Then
    'external address
        If InStrRev(LCase(xRecipientAddress), "@email.domain") = 0 Then
            'Any other SMTP Email domain
            xExternal = 1
        End If
    Else
    
        'catch for outlook nickname cache
        If Left(xRecipientAddress, 6) = "/o=NT5" Then
 
            'Code to get SMTP address from outlook nickname   
    
        End If

    End If

Next

注意@email.domain 在我的代码中更新为我们的 SMTP 域名

如果在收件人列表中找到任何外部收件人,则代码应指定 xExternal = 1。这应包括 Outlook 为其创建 Outlook 昵称的所有收件人以及仅具有 SMTP 地址的收件人。

【问题讨论】:

标签: vba outlook


【解决方案1】:

用途:

If Left(xRecipientAddress, 6) = "/o=NT5" Then

  Debug.Print xRecipients.Item(i).AddressEntry.GetExchangeUser.PrimarySmtpAddress

End If

另外,我不认为/0=NT5 会保持一致,所以可能想要改变它。

【讨论】:

  • 嗨 - 感谢您的回复。这似乎不起作用 - 我只是得到一个空白响应 - AddressEntry 属性返回@之前地址的第一部分,但 PrimarySMTPAddress 属性是空白的。
  • 哦...我无法复制您的问题。否则会有更多帮助。 :)
猜你喜欢
  • 2014-03-03
  • 2011-12-17
  • 1970-01-01
  • 2015-04-30
  • 2022-07-26
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多