Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to introduce the href based mailto link in ASP.NET Code behind(C sharp)

Posted on 2010-09-10
19
Medium Priority
?
1,879 Views
Last Modified: 2012-05-10
I want to incorporate the the mailto link in ASP.NET csharp code behind.
0
Comment
Question by:Ammar Iqbal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 14

Expert Comment

by:existenz2
ID: 33644427
You can create a HtmlAnchor: http://www.w3schools.com/aspnet/control_htmlanchor.asp

and do the following:
HtmlAnchor anchor = new HtmlAnchor();
anchor.Href = "mailto:me@nospam.net";
Page.Controls.Add(anchor);

Open in new window

0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644543
instead of writing the email address directly with mailto , can i use a string variable which contains this emails. smae is the case with subject and body.
All these three things are stored in the respective string varaibles, i nedd to use them in a hregf based mailto link.
Can you provide me with the  complete syntax in C sharp
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33644559
yes, that's no problem:


string emailAddress = "me@nospam.net";
string subject = "Welcome";
string body = "This is my content";
HtmlAnchor anchor = new HtmlAnchor();
anchor.Href = string.Format("mailto:{0}?subject={1}&body={2}",emailAddress,subject,body);
Page.Controls.Add(anchor);

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644632
how to add this control "anchor"  as a link in the ASP.NET page
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644639
Should I convert this in to a string and then dispaly it on the ASP.NET page, so user is able to click on the mailto link
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644672
I am doing this in the catch exception, if for some how , the email could not be sent because of some firewall issues or dns or smtp settings, then i need to give user option option of mailto linkalongwith the email failed message. but by concatenating  " lbEmailStatus.Text = emailfailed + anchor.HRef;". , iam not getting the hyperlink of mailto, instead the tw ostrings are dispalyed


catch (Exception ex)
                   {
                       System.Console.WriteLine(ex.Message);
                       string emailfailed = "The email could not be sent successfully.The credentials of the newly created users are:  user name:'" + userName + "' password:'" + password + "'";
                        HtmlAnchor anchor = new HtmlAnchor();
                        anchor.HRef= string.Format("mailto:{0}?subject={1}&body={2}",toAddress,subject,body);
                        Page.Controls.Add(anchor);
                        lbEmailStatus.Text = emailfailed + anchor.HRef;
                       lbEmailStatus.CssClass = "validate_error";

                   }
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33644691
You need to create a LiteralControl instead of a Label and then write the html in it directly.
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644818
can you give a example
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644862
its not working. same results displaying the whole text instead of showing thel ink for mailto. so that       outlok or anyother email client can pop out
I did like this
ASP.NET page
 <tr>
              <td  align="left" colspan="4">
                  <asp:Label ID="lbEmailStatus" runat="server" EnableViewState="false"> </asp:Label>
              </td>
              <td>
             
                <asp:Literal ID="ltmailto" runat="server" EnableViewState="false"></asp:Literal>
              </td>
             
           </tr>
catch()

{
System.Console.WriteLine(ex.Message);
                       string emailfailed = "The email could not be sent successfully.The credentials of the newly created users are:  user name:'" + userName + "' password:'" + password + "'";
                        HtmlAnchor anchor = new HtmlAnchor();
                        anchor.HRef= string.Format("mailto:{0}?subject={1}&body={2}",toAddress,subject,body);
                        Page.Controls.Add(anchor);
                        lbEmailStatus.Text = emailfailed;
                       lbEmailStatus.CssClass = "validate_error";
                       ltmailto.Text = anchor.HRef;
}
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644954
The whole string is being displayed like this. i do not want this,Infact I require to display mailto as a link and as a result outlook or any toher email client opens up
"
  mailto:ammar.iqbal@gmail.com?subject=Approval&body=Welcome to T4DC Web Module!
Your Account has been approved.
Please Login with the following credentials
Username :'vegar00'
Password : 'baf0f52'
Don't forget to change your password after signing in.  
"
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33644981
put <a href=" "/> around it and put the mailto: content between the ".
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33644995
can you give me the exact syntax. i am trying to do it but....
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33645030
anchor.HRef= string.Format(" <a href=mailto:{0}?subject={1}&body={2},toAddress,subject,body/>");

I am not getting the exact syntax. Can you specify the syntax for me
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33645076
You are mixing up code behind and inline code. What are you using?
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33645118
i am using the code behind code. i neeed to activate the mail to link form the code behind, not to write direct html on the main .aspx page. that is quite simpe. , and is useful when i need to hard codeemail addresses, subject and body.

But  in my case i do not want to hard code toaddresssubject and body.the to address is dynamic address not a hard coded one.i ma not figuring how to incoroporate <a=href ......   with this
anchor.HRef= string.Format(" <a href=mailto:{0}?subject={1}&body={2},toAddress,subject,body/>");

so that mailto link results inan outlook express window popping
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33645141
Then putting the code below in your code behind, preferably pageload or something similar, should be enough to render the mail address correctly as a href tag in the html and open Outlook. No extra coding required.
string emailAddress = "me@nospam.net";
string subject = "Welcome";
string body = "This is my content";
HtmlAnchor anchor = new HtmlAnchor();
anchor.Href = string.Format("mailto:{0}?subject={1}&body={2}",emailAddress,subject,body);
Page.Controls.Add(anchor);

Open in new window

0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 33645172
but this code displays everything in the for mof string, it does not open the outlook window. i do not wnat to invoke this in a page load event. Infact i am doing this in the catch section of my actual sendmail method. When the user fails to send an email because of unknown resons, then i want to provide an additional functionality to then so that they can get an open outlook window, and try to send tan email again thorugh it. this is my intention. for this purpose, i want to associate this anchoe.Href with some label or litral control whose intiialview state is set to false. and which is invoked only when the control enters to the catch section of the original method send email.
0
 
LVL 14

Accepted Solution

by:
existenz2 earned 2000 total points
ID: 33645191
A stupid me! I forgot an important part :) I forgot to set the "Title" property, else you won't see anything.
string emailAddress = "me@nospam.net";
string subject = "Welcome";
string body = "This is my content";
HtmlAnchor anchor = new HtmlAnchor();
anchor.Href = string.Format("mailto:{0}?subject={1}&body={2}",emailAddress,subject,body);
anchor.Title = "Sent email";
Page.Controls.Add(anchor);

Open in new window

0
 
LVL 14

Expert Comment

by:existenz2
ID: 33645199
You can also put the full code in the catch, as long as the PreRender event did not fire yet.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question