【发布时间】: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 地址的收件人。
【问题讨论】:
-
在您知道为什么紧跟
On Error GoTo 0是“强制性”之前,请考虑不要使用On Error Resume Next。