Here I am explaining different ways to create a mailto link. Generally we associate mailto with a hyperlink but we can also associate it with a Button or Image and also open the mailto email window using from server side event.
Simple Mailto
Below is the simplest way to create a mailto using HTML hyperlink.
<a href = "mailto:abc@abc.com">abc@abc.com</a>
Mailto using ASP.Net HyperLink Control
The following describes how to use ASP.Net HyperLink to create a mailto link
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl = "mailto:abc@abc.com" Text = "abc@abc.com"></asp:HyperLink>
Mailto using Image
In order to create mailto link using image there are two ways
1. Using HTML Link
<a href = "mailto:abc@abc.com"><img src = "images/img.jpg" /></a>
2. Using JavaScript
<img src = "images/img.jpg" onclick = "parent.location='mailto:abc@abc.com'"
style ="cursor:pointer" />
Mailto using Button
In order to create a mailto using HTML Button you need to take help of JavaScript
<input id="Button1" type="button" value="button"
onclick = "parent.location='mailto:abc@abc.com'" />
Open Mailto Email Window from Server Side Code
You can also open the mailto email window from Asp.net Server Side Code using ClientScript. Below is my button with click event
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
And below is the code you need to execute
C#
protected void Button1_Click(object sender, EventArgs e)
{
//Do some work
string email = "abc@abc.com";
ClientScript.RegisterStartupScript(this.GetType(), "mailto",
"<script type = 'text/javascript'>parent.location='mailto:" + email +
"'</script>") ;
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Do some work
Dim email As String = "abc@abc.com"
ClientScript.RegisterStartupScript(Me.GetType(), _
"mailto", "<script type = 'text/javascript'>" & _
"parent.location='mailto:" & email & "'</script>")
End Sub
That’s all for now, hope you liked it.