PDA

View Full Version : Auto Block Unwanted Emails with the Blacklist in a Text File



Witzker
11-27-2020, 02:12 AM
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) (https://www.datanumen.com/blogs/how-to-auto-block-unwanted-outlook-emails-with-the-blacklist-in-a-text-file/)
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.

Witzker
11-30-2020, 05:19 PM
In combination with this:

Write the senders Email to a text file (fighting spam) (vbaexpress.com) (http://www.vbaexpress.com/forum/showthread.php?68094-Write-the-senders-Email-to-a-text-file-(fighting-spam)&p=405661#post405661)

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

Witzker
12-01-2020, 01:52 PM
Maybe also better to use an EXCEL file for BlackList?
What do You Think?

gmayor
12-01-2020, 09:53 PM
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.

Witzker
12-02-2020, 03:21 AM
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?

Witzker
12-02-2020, 07:31 AM
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?