F 505
F 505

Reputation: 414

How to include link in a mail body message?

I want to send an email with a hyperlink, I tried to send the email without the link and its worked, but when I add the link it gives an error

This is my code:

MailMessage o = new MailMessage("f@hotmail.com", "f@hotmail.com", "KAUH Account Activation", "Hello, " + name + "\n Your KAUH Account about to activate click the link below to complete the actination process \n "+<a href=\"http://localhost:49496/Activated.aspx">login</a>);
NetworkCredential netCred = new NetworkCredential("f@hotmail.com", "****");
SmtpClient smtpobj = new SmtpClient("smtp.live.com", 587);
smtpobj.EnableSsl = true;
smtpobj.Credentials = netCred;
smtpobj.Send(o);

Upvotes: 8

Views: 49273

Answers (6)

Code
Code

Reputation: 749

string url = lblHidOnlineURL.Value + hidEncryptedEmpCode.Value;
body = hid_EmailBody.Value.Replace("@Compting", "HHH").Replace("@toll", hid_TollFreeNo.Value).Replace("@llnk", "<a style='font-family: Tahoma; font-size: 10pt; color: #800000; font-weight: bold' href='http://" + url + "'>click here To Download</a>");

Upvotes: 0

Mikael &#214;stberg
Mikael &#214;stberg

Reputation: 17176

You need to enable HTML for the body of the MailMessage like so:

o.IsBodyHtml = true;

Maybe you should choose another constructor, to make the code more readable. Something like this perhaps:

var mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender@domain.com", "Customer Service");
mailMessage.To.Add(new MailAddress("someone@domain.com"));
mailMessage.Subject = "A descriptive subject";
mailMessage.IsBodyHtml = true;
mailMessage.Body = "Body containing <strong>HTML</strong>";

Full docs: http://msdn.microsoft.com/en-us/library/System.Net.Mail.MailMessage(v=vs.110).aspx

Update It seems like it is your string building that cause you trouble. Sometimes, when putting strings together (or concatenating them as it is called) it is tricky to get all quotes correct. When creating such a large string as an email, there are some options to get it right.

First, regular string - downside is that it's hard to read

string body = "Hello, " + name + "\n Your KAUH Account about to activate click the link below to complete the actination process \n <a href=\"http://localhost:49496/Activated.aspx">login</a>";

Second, verbatim string - allows line breaks in the code which improved readability. Note the @ character in the beginning and that the quote escape sequence changed from \" to "".

string body = @"Hello, " + name + "\n Your KAUH Account about to
    activate click the link below to complete the actination process \n 
    <a href=""http://localhost:49496/Activated.aspx"">login</a>"

Third, string builder. This is actually the preferred way in many regards.

var body = new StringBuilder();
body.AppendFormat("Hello, {0}\n", name);
body.AppendLine(@"Your KAUH Account about to activate click 
    the link below to complete the actination process");
body.AppendLine("<a href=\"http://localhost:49496/Activated.aspx\">login</a>");
mailMessage.Body = body.ToString();

StringBuilder docs: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx

Upvotes: 17

reza jafari
reza jafari

Reputation: 1336

     String body = "ur message : <a href='http://www.yoursite.com'></a>"
     o.Body = body;

o.IsBodyHtml = true

Upvotes: 3

Marc B
Marc B

Reputation: 360862

Syntax error:

MailMessage o [...snip...] \n "+<a href=\"http://localh [...snip...]
                              ^--terminates the string
                                ^^^^^^^^^^^^^^--interpreted as code

Upvotes: 1

GuiDrn
GuiDrn

Reputation: 333

you forget to escape the " : href=\" .... \">login

Upvotes: 1

Konstantin
Konstantin

Reputation: 3294

mark message as html o.IsBodyHtml = true

Upvotes: 3

Related Questions