Help prevent spam bots from getting your email addresses
There is no full proof way of preventing companies from harvesting email addresses from websites. There are few techniques that can be used to help minimize the spam.
There are several JavaScript approaches out there that work well. They usually consist of breaking apart the address into components so there is no “name@somedomain.com” anywhere in the HTML.
For example:
<SCRIPT LANGUAGE="JavaScript">
var user = 'somename';
var site = 'ratchet.com';
document.write('<a href=\"mailto:' + user + '@' + site + '\">');
document.write(user + '@' + site + '</a>');
</SCRIPT>
I tried a different approach with ratchet.com. I used a script to dynamically generate an image of any text with some logic to handle emails. There are several parameters that can be set to control , size, font, and colors.
These emails below are dynamically generated jpgs and demonstrates some of the display options. .NET has decent graphics libraries and there are many ways to build the image out.
The images above are generated by a URL with a query string in an img src tag like:
http://SOME_URL/controls/writetextasimage.ashx?text=daffy.twinkle&fontname=times&fontsize=12&forecolor=%230000ff&backcolor=%23ffffff&fontstyle=italic&isemail=true
By having a basic URL structure it makes it easy to integrate with existing website code.
The .NET server side code is basic. I found some code online and added more logic to it. There are many different directions you could go on the server side like web services, WCF, REST API, etc. There are many more options that could be added like output type (png, bmp, jpg, etc).
Bellow is the main guts of the code. There are several helper methods and properties to process the query string data.
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
// fake bitmap to get needed size
Bitmap bitmap = new Bitmap(1, 1);
int width = 0;
int height = 0;
// create font object with requested properties
Font font = new Font(fontName,
fontSize,
fontStyle);
// create the graphics object to measure text size and perform actual rendering
Graphics graphics = Graphics.FromImage(bitmap);
// get needed size to render the wanted text
SizeF size = graphics.MeasureString(text, font);
width = (int)size.Width;
height = (int)size.Height;
// release resources
graphics.Dispose();
bitmap.Dispose();
// create the actual bitmap with final size
bitmap = new Bitmap(width, height);
// render text
graphics = Graphics.FromImage(bitmap);
graphics.Clear(backColor);
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(text,
font,
new SolidBrush(foreColor),
0,
0);
graphics.Flush();
// create codec object for jpg format
ImageCodecInfo jpegCodec = null;
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
if (codec.MimeType == "image/jpeg")
{
jpegCodec = codec;
break;
}
// set codec quality
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);
// output the binary data to the output stream
bitmap.Save(context.Response.OutputStream, jpegCodec, parameters);
// release resources
graphics.Dispose();
bitmap.Dispose();
}