Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What is the best way to check for multiple black-list email addresses before sending?

I have several email addresses I am not allowed to send information to as part of a project. I want Outlook to check for any of the black-list email addresses and notify me if they are included before sending. Below is the code I found to modify

For example, my black-list includes: "bad@address.com", "worst@address.com", "evil@address.com"

What is the best way to put these addresses into the code below, would be good for it to be in a way that allows for changing the addresses in the black-list easily?

So here is the latest version of my code with your suggestions. Unfortunately it lets me send the emails to the addresses on the Checklist. Any suggestions?

  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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
177 views
Welcome To Ask or Share your Answers For Others

1 Answer

Create a procedure level variable CheckList which is the list of csv of black listed emails. you can initialise this in the procedure as a hard assignment or dynamically retrieve from other data sources for e.g. sql server

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...