Get rid of spam in outlook

How to deal with annoying emails that slip through the spam filter when using Outlook

Are you tired of constantly being bombarded with annoying emails that are not quite spam but still manage to sneak through the filters? Whether it’s from China suppliers, web developers claiming to have found mistakes on your website, or other scammers, these emails can be a major nuisance. Fortunately, there are steps you can take to reduce the frequency of these emails and keep your inbox organized.

  1. Use the “Junk Email” feature in Outlook

Outlook has a built-in feature called “Junk Email” that can help filter out unwanted messages. This feature uses a variety of techniques to identify and block spam, such as analyzing the sender’s address and the content of the message. To enable this feature, go to the “Home” tab in Outlook, select “Junk” from the “Delete” group, and choose “Junk E-mail Options.” From there, you can choose the level of filtering you want and add specific email addresses or domains to your blocked list.

  1. Set up rules to automatically move or delete certain emails

Outlook also allows you to set up rules that will automatically move or delete certain emails based on specific criteria. For example, you could create a rule that moves all emails from a particular sender to a separate folder, or that deletes any emails containing certain keywords. To set up rules in Outlook, go to the “Home” tab, select “Rules” from the “Move” group, and choose “Create Rule.” From there, you can choose the conditions and actions for your rule.

  1. Be cautious when giving out your email address

One of the best ways to avoid receiving unwanted emails is to be careful about who you give your email address to. If you’re signing up for a service or making a purchase online, make sure to read the privacy policy and check for any checkboxes that indicate you will receive promotional emails. You can also consider creating a separate email address specifically for online purchases or for use on public forums.

  1. Use a third-party spam filter

If you’re still receiving a lot of unwanted emails despite your best efforts, you may want to consider using a third-party spam filter. There are many options available, such as SpamAssassin, SpamTitan, and MailWasher, that can integrate with Outlook and provide more advanced filtering options. These filters use more sophisticated algorithms to identify spam and can often catch emails that Outlook’s built-in filter might miss.

By following these tips, you can reduce the number of annoying emails that slip through the cracks and keep your Outlook inbox organized and clutter-free.

If the above is not enough – we can choose the nuke option!

Allow Deny Outlook custom e-mail filtering - Get rid of spam in outlook
Allow Deny Outlook custom e-mail filtering

We can take advantage of the VBA scripting options to make a list that will allow or deny e-mail addresses.

The script is designed to allow Outlook users to manage and filter their emails more effectively, particularly those that slip through the spam filter. It allows users to either “Allow” or “Disallow” certain email senders, which will either allow or block their emails from appearing in the inbox.

The script is split into two parts – a standard module and a class module. The standard module contains the main procedures for “Allowing” or “Disallowing” emails, while the class module contains the necessary event handlers for processing new emails as they arrive in the inbox.

To implement the script, follow these steps:

  1. Open Outlook and press ALT + F11 to open the VBA editor.
  2. In the editor, create a new standard module and paste the script code into it.
  3. Save the module with a descriptive name (e.g. “AllowDisallowEmails”).
  4. Create a new class module and name it “clsInboxHandler”. Paste the code into it.
  5. Save the class module and close the editor.
  6. Go back to Outlook and open the “Rules and Alerts” window.
  7. Create a new rule that triggers on the arrival of a new email.
  8. In the “Select Condition(s)” window, select “with specific words in the sender’s address”.
  9. In the “Specify Words or Phrases” window, add a wildcard character (*) to match any sender address, and click “OK”.
  10. In the “Select Actions” window, select “run a script” and choose the “AllowEmail” or “DisallowEmail” procedure from the dropdown list.
  11. Name the rule and click “Finish”.

Once the rule is created, it will automatically trigger whenever a new email arrives in the inbox. The script will then evaluate the sender’s email address and decide whether to “Allow” or “Disallow” the email based on whether the sender is on the corresponding list.

Here’s a breakdown of what the script code does:

  • The three constants at the top of the script define the paths to the files that store the allow and disallow lists, as well as the log file that records the script’s activity.
  • The “inboxHandler” object is created as a new instance of the “clsInboxHandler” class, which will handle the processing of new emails as they arrive.
  • The “Application_Startup” procedure initializes the “inboxHandler” object and sets it up to handle incoming emails.
  • The “AllowEmail” and “DisallowEmail” procedures are called when the corresponding rules trigger. They both retrieve the currently selected email in the Outlook explorer window and pass it to the “ProcessEmail” procedure in the “inboxHandler” object.
  • The “clsInboxHandler” class module contains the main logic for evaluating incoming emails and deciding whether to “Allow” or “Disallow” them.
  • The “Initialize” procedure sets up the event handlers for the “inboxItems” object, which is a collection of items in the inbox.
  • The “inboxItems_ItemAdd” event handler is triggered whenever a new email is added to the inbox. It checks whether the added item is an email, and if so, it passes it to the “ProcessEmail” procedure for evaluation.
  • The “ProcessEmail” procedure evaluates the email sender’s address and determines whether they are on the “allow” or “disallow” list. If the sender is on the allow list, the email is allowed to pass through to the inbox, and if the sender is on the disallow list, the email is deleted. If the sender is not on either list, a decision prompt is displayed asking the user whether to add the sender to the allow or disallow list.
  • The “DisplayDecisionPrompt” function is called when an email sender is not on either the allow or disallow list. It displays a prompt asking the user whether to add the sender to the allow or disallow list. If the user selects “allow”, the email is allowed to pass through to the inbox, and the sender is added to the allow list. If the user selects “disallow”, the email is deleted, and the sender is added to the disallow list.
  • The “LoadList” function loads the allow and disallow lists from their respective files and returns them as a collection object.
  • The “AddToList” procedure adds a new email address to the specified list file. If the file does not exist, it is created.
  • The “LogEvent” procedure writes a message to the log file, including the current date and time.

The script is designed to be flexible, allowing users to customize the paths to the list and log files as needed. By default, the script saves these files in the user’s “AppData\Roaming\Microsoft\Signatures” folder, but this can be changed to any other location.

Overall, this script can be a useful tool for anyone who receives a lot of unwanted emails that slip through the spam filter. By allowing users to easily add senders to an allow or disallow list, the script can help reduce the clutter in the inbox and make it easier to manage emails efficiently.

And the code:
1st file is a “Module” –> Insert:

Option Explicit

Const ALLOW_LIST_FILE As String = "C:\Your-path\allow_list.txt"
Const DISALLOW_LIST_FILE As String = "C:\Your-path\disallow_list.txt"
Const LOG_FILE As String = "C:\Your-path\log_file.txt"


Dim inboxHandler As New clsInboxHandler

Sub Application_Startup()
    inboxHandler.Initialize
End Sub

Sub AllowEmail()
    Dim mail As Outlook.MailItem
    Set mail = Application.ActiveExplorer.Selection.Item(1)
    If Not mail Is Nothing Then
        inboxHandler.ProcessEmail mail, True
    End If
End Sub

Sub DisallowEmail()
    Dim mail As Outlook.MailItem
    Set mail = Application.ActiveExplorer.Selection.Item(1)
    If Not mail Is Nothing Then
        inboxHandler.ProcessEmail mail, False
    End If
End Sub

2nd file is “Class Module”:

Option Explicit

Const ALLOW_LIST_FILE As String = "C:\Users\Miran\AppData\Roaming\Microsoft\Signatures\allow_list.txt"
Const DISALLOW_LIST_FILE As String = "C:\Users\Miran\AppData\Roaming\Microsoft\Signatures\disallow_list.txt"
Const LOG_FILE As String = "C:\Users\Miran\AppData\Roaming\Microsoft\Signatures\log_file.txt"

Private WithEvents inboxItems As Outlook.Items

Public Sub Initialize()
    Dim ns As Outlook.NameSpace
    Set ns = Application.GetNamespace("MAPI")
    Set inboxItems = ns.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Class_Initialize()

End Sub
Private Sub inboxItems_ItemAdd(ByVal Item As Object)
    On Error Resume Next
    If Item.Class = olMail Then
        Dim mail As Outlook.MailItem
        Set mail = Item
        ProcessEmail mail ' <-- Make sure this line passes the 'mail' variable
    End If
End Sub

Sub ProcessEmail(mail As Outlook.MailItem, allow As Boolean)
    Dim senderEmail As String
    Dim allowList As Collection, disallowList As Collection
    Dim decision As Variant
    Dim inAllowList As Boolean, inDisallowList As Boolean

    senderEmail = mail.SenderEmailAddress
    Set allowList = LoadList(ALLOW_LIST_FILE)
    Set disallowList = LoadList(DISALLOW_LIST_FILE)

    On Error Resume Next
    inAllowList = (allowList.Item(senderEmail) <> "")
    On Error GoTo 0

    On Error Resume Next
    inDisallowList = (disallowList.Item(senderEmail) <> "")
    On Error GoTo 0

    If inAllowList Then
        If inDisallowList Then
            MsgBox "This email is in both the Allow and Disallow lists. Prioritizing the Allow list.", vbInformation
        End If
        LogEvent "Allowed: " & senderEmail
        Exit Sub
    ElseIf inDisallowList Then
        mail.Delete
        LogEvent "Blocked: " & senderEmail
        Exit Sub
    End If

    If allow Then
        AddToList ALLOW_LIST_FILE, senderEmail
        LogEvent "Allowed: " & senderEmail
    Else
        AddToList DISALLOW_LIST_FILE, senderEmail
        mail.Delete
        LogEvent "Blocked: " & senderEmail
    End If
End Sub


Function DisplayDecisionPrompt(mail As Outlook.MailItem, allow As Boolean) As Variant
    Dim prompt As String
    If allow Then
        prompt = "Sender: " & mail.SenderEmailAddress & vbCrLf & _
             "Subject: " & mail.Subject & vbCrLf & _
             "Body: " & Left(mail.Body, 300) & vbCrLf & vbCrLf & _
             "Press 1 to Allow, 2 to Disallow"
    Else
        prompt = "Sender: " & mail.SenderEmailAddress & vbCrLf & _
             "Subject: " & mail.Subject & vbCrLf & _
             "Body: " & Left(mail.Body, 300) & vbCrLf & vbCrLf & _
             "Press 1 to Disallow, 2 to Allow"
    End If
    DisplayDecisionPrompt = InputBox(prompt, "Email Allow/Disallow Decision")
End Function

Function LoadList(filePath As String) As Collection
    Dim fso As Object, file As Object, line As String
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set LoadList = New Collection

    If fso.FileExists(filePath) Then
        Set file = fso.OpenTextFile(filePath, 1)
        Do While Not file.AtEndOfStream
            line = file.ReadLine
            On Error Resume Next
            LoadList.Add line, line
            On Error GoTo 0
        Loop
        file.Close
    End If
End Function

Public Sub AddToList(filePath As String, email As String)
    Dim fso As Object, file As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    If Not fso.FileExists(filePath) Then
        Set file = fso.CreateTextFile(filePath)
        file.Close
    End If

    Set file = fso.OpenTextFile(filePath, 8)
    file.WriteLine email
    file.Close
End Sub

Sub LogEvent(message As String)
    Dim fso As Object, file As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    If Not fso.FileExists(LOG_FILE) Then
        Set file = fso.CreateTextFile(LOG_FILE)
        file.Close
    End If

    Set file = fso.OpenTextFile(LOG_FILE, 8)
    file.WriteLine Now & ": " & message
    file.Close
End Sub


Private Sub inboxItems_ItemChange(ByVal Item As Object)

End Sub

Private Sub inboxItems_ItemRemove()

End Sub

Leave a Reply

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