从 Outlook 昵称中提取 SMTP 地址



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

我发现 Outlook 已分配为 Outlook 昵称的收件人无法使用 SMTP 地址进行解析。

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

/

o=NT5/ou=

00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

我无法从中提取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地址的收件人。

使用:

If Left(xRecipientAddress, 6) = "/o=NT5" Then
  Debug.Print xRecipients.Item(i).AddressEntry.GetExchangeUser.PrimarySmtpAddress
End If

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

最新更新