ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Different ways to create mailto: link - ASP.Net
Author Name: Mudassar Khan Published Date: July 05, 2009
Filed Under :
ASP.Net
Views: 5369

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



Different ways to create a mailto: link in asp.net

The above code has been tested in the following browsers

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.


That’s all for now, hope you liked it.


 

If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code