Consulting

Results 1 to 6 of 6

Thread: Auto Block Unwanted Emails with the Blacklist in a Text File

  1. #1
    VBAX Regular
    Joined
    May 2018
    Posts
    50
    Location

    Question Auto Block Unwanted Emails with the Blacklist in a Text File

    As I get a lot of spams every Day I'm looking for a method to handle this easily.
    I have may POP accounts in a single PST file.
    As I learned that Outlook has blacklists for every account the "Block Sender" function in Outlook does not work!
    In addition, I have 3 Outlooks synct via SimpleSyn so if I block one sender in 1st Ol 2nd Ol will not know This and receive the mail which is the synct to the 2 Other OLs. CLEAR?
    So I'm looking For a method to block senders in this Scenario.

    What I found is This Macro
    Public WithEvents objInboxFolder As Outlook.FolderPublic 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
                    Next
                 End If
               End If
           Loop
     
        End If End Sub
    From Here:
    How to Auto Block Unwanted Outlook Emails with the Blacklist in a Text File - Data Recovery Blog (datanumen.com)
    I must say I'm not clever enough to understand how it works BUT it Does.
    In BlackList.txt I have:
    name@domain.com
    As I receive a lot of spam from the same domain It is necessary to have the Domain itself blocked.
    If I change the content of the Blacklist.txt file to
    *@domain.com Or
    @domain.com
    The macro does not work anymore.
    So my question is
    How can the macro be modified so that it would also work if in the list is either
    name@domain.com
    or/ and
    *@domain.com Or @domain.com
    Hope for help.

  2. #2
    VBAX Regular
    Joined
    May 2018
    Posts
    50
    Location
    In combination with this:

    Write the senders Email to a text file (fighting spam) (vbaexpress.com)

    It could be a great solution to fighting spam!
    What do you think?

  3. #3
    VBAX Regular
    Joined
    May 2018
    Posts
    50
    Location
    Maybe also better to use an EXCEL file for BlackList?
    What do You Think?

  4. #4
    You would have to do some tests to confirm, but I suspect there is little difference in speed between reading a text file and an Excel file.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    May 2018
    Posts
    50
    Location
    Quote Originally Posted by gmayor View Post
    You would have to do some tests to confirm, but I suspect there is little difference in speed between reading a text file and an Excel file.
    THX for taking care
    How can I help testing?

  6. #6
    VBAX Regular
    Joined
    May 2018
    Posts
    50
    Location
    Quote Originally Posted by Witzker View Post
    THX for taking care
    How can I help testing?
    My suggestion would be to
    First change the macro that it also could read entries like
    name@domain.xx = OK
    *@domain.com = to be added
    and also
    *@*.domain.com = to be added
    because I noticed that spammer use often the name@xxxx.domain.xx

    and put them into spam folder.

    What do you think?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •