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);
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
mark message as html o.IsBodyHtml = true
String body = "ur message : <a href='http://www.yoursite.com'></a>"
o.Body = body;
o.IsBodyHtml = true
you forget to escape the " : href=\" .... \">login
Syntax error:
MailMessage o [...snip...] \n "+<a href=\"http://localh [...snip...]
^--terminates the string
^^^^^^^^^^^^^^--interpreted as code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With