Click here to Skip to main content
12,952,792 members (34,447 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 12 Jul 2012

MVC Extension: Mailto

, 12 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Don't write your email in plain text when creating mailto-links


If you don't want your Outlook filled with spam, you should be careful with mailto-links on your website. This extension will scramble your email address.

Using the Code

@Html.Mailto("", "display name")   

The Extension Method

public static MvcHtmlString Mailto(this HtmlHelper helper, 
		string emailAddress, string displayText = null)
    if (string.IsNullOrEmpty(displayText))
        displayText = emailAddress;

    var sb = string.Format("<a href=\"{0}{1}\" title=\"{1}\">{2}</a>", 
    		CharEncode("mailto:"), CharEncode(emailAddress), CharEncode(displayText));
    return new MvcHtmlString(sb);

And you will need CharEncode() function as well. Put it in a common library.

public static string CharEncode(string value)
    var enc = Encoding.Default;
    var retval = "";
    for (var i = 0; i < value.Length; i++)
        retval += "&#" + enc.GetBytes(new[] 
        	{Convert.ToChar(value.Substring(i, 1))})[0] + ";";
    return retval;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Aurum AS
Norway Norway
Microsoft Certified Solutions Developer (MCSD)

Personal website:

My projects: - A free to use norwegian crm software
Fakturax - A free to use norwegian invoice software
Timefø - A free to use norwegian timereg software
MittUtlegg - A free to use norwegian software for receipts
SupportWeb - A free to use norwegian software customersupport

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 12 Jul 2012
Article Copyright 2012 by StianSandberg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid