I’ve found this small code that sends email to gmail users. I’d like the body of the mail to contain html (for example, decoding a link for it to hold different text than the url it’s pointing to).
I am using c# .net 3.5. I’ve used these classes in my code:
- MailMessage
- SmtpClient
How can this be done?
Here’s a copy of my code:
MailMessage message = new MailMessage("me@gmail.com", WebCommon.UserEmail, "Test", context.Server.HtmlEncode("<html> <body> <a href='www.cnn.com'> test </a> </body> </html> "));
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("him@gmail.com", "myPwd");
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = cred;
smtp.Port = 587;
smtp.Send(message);
Thanks!
Something like this should work:
Note that
MailMessagerefers toSystem.Net.MailMessage. There is alsoSystem.Web.MailMessage, which I have never used and -as far as I know- is obsolete.For more sophisticated templating solutions that render the Body html rather than hard-codin it, there is, for example, the
EMailTemplateServicein MvcContrib which you can use as a guideline.