This is a follow up to the initial Spam Email Filtering – Part 1 post. It has been a little over a month since I first developed the functionality and I’ve been very happy with the results. Since then, I’ve made a few tweaks to it. The download links in both the initial post and this one are to the same files so it doesn’t matter from which post you download them, and they are the latest version.
Latest updates:
- Added PurgeDeletedItems() – this only deletes spam emails from the Deleted Items email folder. This was added because when spam is detected in the Inbox, it is moved to the Deleted Items email folder. Normally, email stays in this folder for 30 days before Outlook will automatically delete it. Since we are managing known spam, why let it sit for 30 days?
- Added MoveJunk2Inbox() – this was added to optimize the spam filtering process and eliminate the need to manually manage the Junk email folder. This is also helpful when Outlook inadvertently sends legitimate email to the Junk folder; the function moves anything sent to the Junk folder back to the Inbox.
- Updated ThisOutlookSession macro to call the MoveJunk2Inbox() process to automate the management of the Junk email folder.
The below text file contains the known spam domains and has been updated since the initial version. The list of known spam domains will grow as you identify new spam email and place them in the BadEmail folder. See part 1 for more details.
Current version of all the spam filtering functionality.
Current version that automatically moves email from the Junk folder and runs the spam filtering process when new email arrives in the Inbox.
Recap Of The Setup Process
Step 1 – Create a folder on the C: drive named _spam_domains and place the downloaded spamDomainsList.txt file in this location; the spam filter macro expects to find the text file at this location. You can use a different location and rename the file if you like, but if you do, you’ll need to edit the macro every place that references the default values.
Step 2 – Select the Developer menu option from the top menu bar of Outlook. If you don’t see it, a quick Google search will explain how to enable and display that feature. Selecting the Developer menu option will open the Visual Basic interface which allows you to create and manage macros. Create a new module and then copy/paste the contents from the spamFilter.txt file you downloaded and save. Once this step is completed, you have everything needed to manually manage spam email.
Step 3 – To automate the filtering process, select the Developer menu option (if not already open) and select ‘Visual Basic’ to open the macro editor. Next, double-click the ThisOutlookSession to open the editor panel. Then copy/paste the contents of the ThisOutlookSession.txt file you downloaded and save. Finally, close Outlook and reopen it and the macros will now automatically process spam email as it arrives.
For a more detailed explanation of the setup steps, refer to the Spam Email Filtering – Part 1 post.
Helpful Notes
The spam filtering process only runs if Outlook is open. If you close Outlook, any received spam email will be in your Inbox until you either receive a new email or manually run the macro.
The filtering process only applies to emails that are Unread. If you view a suspected spam email and the status changes to Read, the spam filter will ignore it unless you change the status back to Unread.
In the ProcessBadEmailFolder() process, make sure to update the following line with your actual email address:
Set badFolder = olNs.Folders(“your_Outlook_email“).Folders(“BadEmail”) ‘ Replace your_Outlook_email with your actual Outlook email address.
In the event you accidentally placed a “good” email in the BadEmail folder and it gets added to the spamDomainsList.txt file, you can remove it. Simply open the text file and delete the domain you don’t want filtered and save the file. It’s that simple.
The PurgeDeletedItems() can be disabled if you don’t want spam emails to be automatically deleted. When new email arrives, if the spam filter identifies it as spam it moves the email to the Deleted Items email folder. After all Unread emails in the Inbox have been processed and the ProcessBadEmailFolder() process has run, the PurgeDeletedItems() process then runs. It will only delete Unread spam email found in the spamDomainsList.txt file. If you want to disable this process, comment out the Call PurgeDeletedItems statement in the macro module that was copied from the spamFilter.txt file.
In Outlook, you can customize the Quick Access menu bar by adding mini icons to specific processes of the spam filter macro. I created one that calls the CheckInbox() process which runs the complete spam filter routine and displays a confirmation when done. I’ll occasionally use it if I want to immediately run the process to add new spam emails that I placed in the BadEmail folder. The second one I created calls the CountSpamDomains() that is only for informational purposes and displays a message box with the total number of spam domains contained in the spamDomainsList.txt file. The list will grow over time as you identify new spam and place them in the BadEmail folder.
Let me know if you find this useful or think of something that could be added to it. Enjoy…