ASP.NET – Use ASHX handler to protect your email from spam

What happens to your email after a week of publishing into internet, I am sure you will get some of spam mails in your inbox. You may be curious how does the spammer knows your email because you just post it in a website and give it your friends, for example my email at my about page ? The answer lies at email bot. This bot will crawl through out a web site and use its technique, for example regular expression, to detect only email addresses in content and insert these emails into database for spamming later.
So how can we solve this problem? I would like to publish my email for contact over internet but I want to keep it safe apart from spammers. Converting email address into image to avoid spammer must be an intelligent idea which is already used in many web applications. In the example below I would like to show a simple way to generate image from text with ASP.NET and show it in our website.

1. Create an ASP.NET Application “ASHX Handler in ASP.NET”
2. Design GUI as following

3. Besides label Email, insert an Image control.
4. Create a new generic handler, name it “EmailHandler.ashx”.

5. Insert the code below into handler file to generate image from our email text

public void ProcessRequest(HttpContext context)
	string strDisplay = "";
	Bitmap bmpOut = new Bitmap(200, 18);
	Graphics g = Graphics.FromImage(bmpOut);
	g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
	g.FillRectangle(Brushes.AliceBlue, 0, 0, 200,18);
	g.DrawString(strDisplay, new Font("Verdana", 10), new SolidBrush(Color.Aqua), 0, 0);
	MemoryStream ms = new MemoryStream();
	bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
	byte[] bmpBytes = ms.GetBuffer();
	context.Response.ContentType = "image/png";


public bool IsReusable
		return false;

6. Set ImageUrl of Image Control to our email handler EmailHandler.ashx

7. View the website in browser and see the result

Instead of using ASHX handler you can use ASP.NET Page to give an equivalent result. However a ASP.NET web forms inherit from the Page class. This provides them with many events and a very detailed initialization and event model. You don’t need that for dynamic images, XML, or binary files.

Use Web Forms: If you have simple HTML pages
ASP.NET custom controls
Simple dynamic pages

Use handlers: If you have binary files
Dynamic image views
Performance-critical web pages
XML files
Minimal web pages

The complete source code you can download here “ASHX handler in ASP.NET

Leave a comment

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