How to Auto Block Unwanted Outlook Emails with the Blacklist in a Text File

Perhaps you’ve stored a blacklist of email addresses in a Plain Text file. Now, if you wish Outlook to auto block the emails with the blacklist, you can utilize the method introduced in this article.

To auto move the emails from specific senders to “Junk E-mail” folder in Outlook, you can use Outlook rule. It demands you to manually enter the specific sender email addresses in the rule condition. If you only want to block few senders, you can just use Outlook rule. But, if you hope to block the emails with a blacklist in a plain text file, Outlook rule cannot help you. Therefore, you ought to seek other means, such as the following one.

Auto Block Unwanted Emails with the Blacklist in a Text File

  1. To begin with, launch Outlook application.
  2. Then, trigger Outlook VBA editor with reference to “How to Run VBA Code in Your Outlook“.
  3. Subsequently, copy the following code into the “ThisOutlookSession” project.
Public WithEvents objInboxFolder As Outlook.Folder
Public WithEvents objInboxItems As Outlook.Items
Public objJunkFolder As Outlook.Folder

Private Sub Application_Startup()
    Set objInboxFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
    Set objInboxItems = objInboxFolder.Items
    Set objJunkFolder = Outlook.Application.Session.GetDefaultFolder(olFolderJunk)
End Sub

Private Sub objInboxItems_ItemAdd(ByVal objItem As Object)
    Dim objMail As Outlook.MailItem
    Dim strSenderEmailAddress As String
    Dim strTextFile As String
    Dim objFileSystem As Object
    Dim objTextStream As Object
    Dim objRegExp As Object
    Dim objMatches As Object
    Dim objMatch As Object
    Dim strLine As String

    If TypeName(objItem) = "MailItem" Then
       Set objMail = objItem
       strSenderEmailAddress = objMail.SenderEmailAddress
       'Change the path to the specific plain text file
       strTextFile = "E:\BlackList.txt"
       Set objFileSystem = CreateObject("Scripting.FileSystemObject")
       Set objTextStream = objFileSystem.OpenTextFile(strTextFile)
       'Get email addresses in the plain text file
       Set objRegExp = CreateObject("vbscript.RegExp")
       With objRegExp
            .Pattern = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|""(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*"")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"
            .IgnoreCase = True
            .Global = True
       End With
       Do Until objTextStream.AtEndOfStream
          strLine = objTextStream.ReadLine
          If strLine <> "" Then
             If objRegExp.test(strLine) Then
                Set objMatches = objRegExp.Execute(strLine)
                For Each objMatch In objMatches
                    If objMatch.Value = strSenderEmailAddress Then
                       objMail.Move objJunkFolder
                       Exit Do
                    End If
             End If
           End If
    End If
End Sub

VBA Code - Auto Block Unwanted Emails with the Blacklist in a Text File

  1. After that, click into “Application_Startup” subroutine and click “Run” icon in the toolbar.
  2. From now on, the incoming email, whose sender is in the specific blacklist, will be automatically moved to the “Junk E-mail” folder.Auto Blocked Email

Restore Corrupted Outlook Data

When coming across Outlook corruption, you have to calm down and take some measures. For instance, you ought to firstly exit Outlook. Then, you can make use of the inbox fix tool, Scanpst.exe to recover the compromised Outlook data. But, if it fails, you can proceed to employ a remarkable and proficient external tool, such as DataNumen Outlook Repair.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including damaged mdf and outlook repair software products. For more information visit

Leave a Reply

Your email address will not be published. Required fields are marked *