Support test
Support test

Reputation: 41

How to correctly combine html in asp.net code?

I'm attempting to send an email using ASP.NET C#. The emails sends just fine. The problem is I am trying to embed a link within the email, but link on the client end is not recognised (as in it cant be recognised as a link by the browser)

I have tried putting the C# into one variable, I have tried to split the code into various variables but I am not too sure what I am doing wrong here. It could be how Im placing the ' or " to embed. not too sure.


string body = "Hello " + firstNameUser + ",";

body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + HttpContext.Current.Request.Url.Authority + "/Auth/Activation/Activation.aspx?ActivationCode=" + TwoFACode + "'>Click here to activate your account.</a>";

body += "<br /><br />Thanks";
body += "<br /><br />Support Team";

mail.Body = body;
mail.IsBodyHtml = true;
client.Send(mail);

'Please click the following link to activate your account' Cant be recognised meaning its not a clickable HTML Link. On the email all you can see is just text instead of a hyperlink.

Upvotes: 1

Views: 565

Answers (1)

KH S
KH S

Reputation: 444

It looks to me that you are missing something in the link:

body += "<br /><a href = 'https://" + HttpContext.Current.Request.Url.Authority + 
"/Auth/Activation/Activation.aspx?ActivationCode=" + 
TwoFACode + "'>Click here to activate your account.</a>";

Adding the needed https:// or http:// should fix it.

Uri.Authority Property

The Authority property is typically a server DNS host name or IP address. 
This property might include the service port number if it differs from the 
default port for the URI. If the Authority component contains reserved 
characters, these are escaped in the string value returned by this property.

Hope this helps.

Upvotes: 1

Related Questions