Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the code:

MailMessage o = new MailMessage("[email protected]", "[email protected]", "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("[email protected]", "****");
SmtpClient smtpobj = new SmtpClient("smtp.live.com", 587);
smtpobj.EnableSsl = true;
smtpobj.Credentials = netCred;
smtpobj.Send(o);
like image 670
F 505 Avatar asked Nov 18 '13 15:11

F 505


5 Answers

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("[email protected]", "Customer Service");
mailMessage.To.Add(new MailAddress("[email protected]"));
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

like image 144
Mikael Östberg Avatar answered Sep 30 '22 16:09

Mikael Östberg


mark message as html o.IsBodyHtml = true

like image 22
Konstantin Avatar answered Sep 30 '22 18:09

Konstantin


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

o.IsBodyHtml = true
like image 42
reza jafari Avatar answered Sep 30 '22 17:09

reza jafari


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

like image 21
GuiDrn Avatar answered Sep 30 '22 18:09

GuiDrn


Syntax error:

MailMessage o [...snip...] \n "+<a href=\"http://localh [...snip...]
                              ^--terminates the string
                                ^^^^^^^^^^^^^^--interpreted as code
like image 37
Marc B Avatar answered Sep 30 '22 18:09

Marc B