Different ways to create mailto: link - ASP.Net
 
Author:
Filed Under: ASP.Net
Published Date: Jul 05, 2009
Views: 12942
 

Abstract: Here Mudassar Ahmed Khan has explained the different ways one can create a mailto link to open an email window in ASP.Net

Comments:  0

 

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.


 








Related Articles



Comments

No comments have been added to this article.

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.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit