Click here to Skip to main content
16,008,010 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a set of email address for those email address i should not send any mails even though if i enter their mail in "To" field in new mail when i click send key it through a pop up like XXXX mail id is matched in do not contact list which i mentioned it in code.

can anyone please provide me the code to check mail id in the "TO" field and the code.

i tried the below code.
VB
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
  
 Set Recipients = Item.Recipients
  For i = Recipients.Count To 1 Step -1
    Set recip = Recipients.Item(i)
     
 If InStr(LCase(recip), "bad@address.com") 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

Regards
Pradeep
Posted
Updated 29-Oct-15 23:48pm
v2

1 solution

First of all, you have to create new MailItem[^], then you'll be able to send an email[^]. But if you want to catch "sending" event, you're on the right track. Unfortunatelly, you did not provide enough information about "do-not-send-to-these-recipients" list: where and how it was defined. If you would like to check if any recipient email address is equal to "bad@address.com", a code look pretty good, but needs small changes: Obtain the E-mail Address of a Recipient[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900