Saturday, March 31, 2012

String to html conversion

I want to send an email in html format through code. Problem I am having is converting the string into html without and grumps. Does .net have a class that converts this easily. I have tried doing a search and replace chracters, but still having issues. Or has anyone done anything similar?

BC

hi bcanonica,

do you have some sample code on you that i could look into?

regards,

zee


Hello,

By default, email sent with System.Net.Mail is formatted as plain text. To format as Html, set the MailMessage.IsBodyHtml property to true.

[ C# ]

 //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress("me@.mycompany.com"); mail.To.Add("you@.yourcompany.com"); //set the content mail.Subject = "This is an email"; mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>"; mail.IsBodyHtml = true; //send the message SmtpClient smtp = new SmtpClient("127.0.0.1"); smtp.Send(mail);


[ VB.NET ]

 'create the mail messageDim mail As New MailMessage()'set the addressesmail.From = New MailAddress("me@.mycompany.com")mail.To.Add("you@.yourcompany.com")'set the contentmail.Subject = "This is an email" mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>"mail.IsBodyHtml = True'send the messageDim smtp As New SmtpClient("127.0.0.1")smtp.Send(mail)

Not exactly what I was looking for. I want to know how I can convert an html email that I create in Publisher to to a string so I can send it in the body of mailMessage object. Is their a namespace that converts from a string to html and vis versa. Meaning using quotes and other characters that do not match.

BC


You can useServer.HtmlEncode and alsoHttpServerUtility.HtmlEncode Method andHttpServerUtility.HtmlDecode Method.

Here still is another way to do such things:Rendering a control as an Html String

HTH

0 comments:

Post a Comment