Simple Email Spam Filtering with Microsoft C# .NET and JQuery

When I first created the website for my business ABION LLC, I put my email address on the first page, and it was a disaster to put it mildly. I was bombarded with all kinds of spam messages every day and I eventually had to shut down the email account due to all the spam. Now I have several websites, all full feature Microsoft ASP.NET MVC Web Applications with Microsoft SQL Server databases, including the one for ABION LLC. I also have a website for my CV at michaelgworkman.com, and also other example web applications as well like bookz-and-authorz.net All of these are hosted in the Microsoft Azure Cloud at Microsoft's Eastern USA data center. All of them are implemented in the Microsoft C# .NET programming language. Also, all of them use JQuery Javascript framework.

Source code for these web applications, and other software projects I have worked on, is available on the public internet at GitHub.com at the following:

https://github.com/Michael-G-Workman/

What I did was to put my email address on my CV site in this format, not a clickable link:

michael DOT g DOT workman AT gmail DOT COM

But also to make it easier for people to contact me, what I did was create a contact page on my CV site at michaelgworkman.com/Home/Contact/. This contact page is written in C# and uses one of my outlook email address to forward the messages to my gmail email address. For about a year it worked very well with little or no spam, then in the past few months spammers discovered it and started sending me the crudest spam emails like "Women in your Town Available for Sex", and also "First Page on Google SEO", since I am not dating, I am not interested in the women, and also since my web applications are hosted in the Microsoft Azure Cloud, and also since my technology blog is hosted in the Google Cloud, I already have really good Google rankings. I am totally not interested in these emails. I doubt the services offered are in fact real. They could be attempts to deliver malware to our computers where a banking trojan like Zeus is installed on our computer secretly, then next time you login to your banks website, the banking trojan scoops up your password and the next day all your money is gone!!!

Definitely bad news these spam emails, so I took a look at my Contact form software and realized it would be really easy to block the sex dating and google ranking emails actually. Much simpler than I thought it would be at first.

Here in this code block from the Contact view in my MVC web application, the C# software checks to see if the Model state is correct before sending the contact message as an email. My CV web application at michaelgworkman.com was built using the Microsoft Visual Studio Development Environment. Note that there is a variable called "WebMessage" that holds the text of the message being sent.



So, OK, we really do not want the sex emails, and also we do not want the Google SEO emails, so what do we do? One thing I thought about doing was each time I received an email, creating a list of the source email addresses and blocking those emails in the C# code. But that actually seemed like it would be alot of work. What actually would be more simple is to just check the "WebMessage" variable for certain keywords like "Sex", "Adult", "Dating", "Google", "Girls", "SEO", and each time one of these keywords is found in the WebMessage, DO NOT SEND THAT EMAIL. This was not a hard change to make at all, and this is what the C# code looks like after the changes.


However this C# code only works on the server, all web applications also have a client side, which is usually HTML and Javascript, and in the case of my CV site at michaelgworkman.com, I also use JQuery for the form validation. So to be thorough with our Spam Filtering, we should also have filtering on the client side in the JQuery code, this also is not a difficult change, we do the same checks of the WebMessage as in the C# code, but this time in the JQuery code, and when one of the keywords is found, we display a message of INVALID CONTENT and do not submit the form:


So to show this in action, this is what the spammers will see when trying to submit a sex message with the keywords "Hot Girls", they will get an "invalid content" message in red text like the following:


And also in this next example, they will get the same message when trying to submit a message of ADULT DATING:


Now with the simple code changes we have made, it should filter out the majority of the SEX and SEO emails. When the website used gets the INVALID CONTENT message, their message is not forwarded as an email, but is blocked. However, legitimate messages like the following are not blocked and still get sent out:


And then, since this message is not blocked, this is the email that gets sent to my main gmail email address, forwarded from my outlook email address, which the CV web application uses to send emails:

These changes will be very effective at blocking the spam emails being sent by the contact form at my CV web application at michaelgworkman.com. If we wanted to, we could also do filtering of code words in the email, name, and phone fields if we wanted to, but the changes to the message only should be sufficient

Also, you can see many of my software projects on my GitHub source control at https://github.com/Michael-G-Workman/

If you have any questions about software development in general, including mobile apps, web apps, relational databases, Windows, Linux/Unix, and/or safety critical C/C++, please feel free to contact me by email at

michael DOT g DOT workman AT gmail DOT COM

Comments

Popular Posts