In this article I will explain with an example, the different ways to create MailTo link in ASP.Net using C# and VB.Net.
MailTo link is used to open the default email client application present on the user’s machine.
This article will explain, how to associate mailto link in Hyperlink, Button or image.
 
 
Using MailTo Links
1. MailTo using HTML Anchor element
The HTML Markup consists of
anchor – For creating link.
<h3>Using Anchor element</h3>
<a href="mailto:abc@abc.com">abc@abc.com</a>
 
2. MailTo using ASP.Net HyperLink Control
The HTML Markup consists of:
HyperLink – For creating link.
The HyperLink has been assigned with the NavigateUrl property for redirecting to link.
<h3>Using Hyperlink element</h3>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="mailto:abc@abc.com" Text="abc@abc.com"></asp:HyperLink>
 
3. MailTo using HTML Image element
In order to create MailTo link using image there are two ways:-
1. Using HTML Link
<h3>Using HTML Link</h3>
<a href="mailto:abc@abc.com"><img src="images/img.jpg" /></a>
 
2. Using JavaScript
<h3>Using JavaScript</h3>
<img src="images/img.jpg" onclick="parent.location='mailto:abc@abc.com'" style="cursor: pointer" />
 
4. MailTo using HTML Button element
The HTML Markup consists of:
input – For displaying button.
The Button has been assigned with an onclick event handler.
<h3>Using HTML Button</h3>
<input id="Button1" type="button" value="button" onclick="parent.location='mailto:abc@abc.com'" />
 
5. Implement MailTo link to open Default email client from Server Side in ASP.Net
The HTML Markup consists of:
Button – For opening default mail cllient.
The Button has been assigned with an OnClick event handler.
<h3>MailTo link to open from Server Side</h3>
<asp:Button ID="btnMail" runat="server"  Text="Button" OnClick="OnMail" />
 
 
Redirecting To Link
When the Button is clicked, a client side JavaScript is registered, which sets the JavaScript parent.location property to the MailTo link.
C#
protected void OnMail(object sender, EventArgs e)
{
    string email = "abc@abc.com";
    ClientScript.RegisterStartupScript(this.GetType(), "mailto", "parent.location='mailto:" + email + "'", true);
}
 
VB.Net
Protected Sub OnMail(ByVal sender As Object, ByVal e As EventArgs)
    Dim email As String = "abc@abc.com"
    ClientScript.RegisterStartupScript(Me.[GetType](), "mailto", "parent.location='mailto:" & email & "'", True)
End Sub
 
 
Screenshot
Different ways to create mailto: link - ASP.Net
 
 
Browser Compatibility
The above code has been tested in the following browsers only in versions that support HTML5.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 
Downloads