Outlook VBA 验证收件人

Outlook VBA to verify recipient

发送前检查多个黑名单电子邮件地址的最佳方法是什么?

我有几个电子邮件地址,作为项目的一部分,我不允许将信息发送到这些地址。我希望 Outlook 检查任何黑名单电子邮件地址,并在发送前通知我是否包含这些地址。下面是我找到修改的代码

例如我的黑名单包括:"bad@address.com"、"worst@address.com"、"evil@address.com"

将这些地址放入下面的代码中的最佳方式是什么?以允许轻松更改黑名单中的地址的方式对它有好处吗?

这是我的代码的最新版本以及您的建议。不幸的是,它让我可以将电子邮件发送到清单上的地址。有什么建议吗?

  Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

  Dim Recipients As Outlook.Recipients
  Dim recip As Outlook.Recipient
  Dim i
  Dim prompt As String


On Error Resume Next
 ' use lower case for the address
 ' LCase converts all addresses in the To field to lower case

  Checklist = "bad@address.com" & _
            "worst@address.com" & _
            "evil@address.com" '// , _ and so on

 Set Recipients = Item.Recipients
  For i = Recipients.Count To 1 Step -1
    Set recip = Recipients.Item(i)

 If InStr(LCase(recip), LCase(Checklist)) Then
      prompt$ = "You sending this to this to " & Item.To & ". Are you sure you want to send it?"
       If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
         Cancel = True
       End If
  End If

Next i


End Sub

创建一个程序级变量CheckList,它是黑名单电子邮件的csv 列表。您可以在过程中将其初始化为硬分配或从其他数据源动态检索,例如sql 服务器

Dim lbadFound  As Boolean
dim badAddresses as string
lbadFound = False

CheckList = "bad@address.com," & _
            "worst@address.com," &  _
            "evil@address.com" '// , _ and so on

    Set Recipients = Item.Recipients
    For i = Recipients.Count To 1 Step -1
      Set recip = Recipients.Item(i)

      If instr(1,lcase(CheckList),  LCase(recip)) >=1 Then 
          lbadFound = true
          badAddresses  = badAddresses  & recip & & vbcrlf 
      End If

    Next i

    If lbadFound Then
       prompt$ = "You sending this mail to one or more black listed email address(es)" & badAddresses & vbcrlf & " Are you sure you want to send it?"
       If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
         Cancel = True
       End If
    End If