question

MuhammadTaqi-6635 avatar image
0 Votes"
MuhammadTaqi-6635 asked TimonYang-MSFT commented

HttpUtility.HtmlEncode loosing HTML tags in email

I'm trying to send emails which contains html tags using the following code.

             string host = "smtp.office365.com", subject = "", fromAddr = "fromAddress";
             SmtpClient client = new SmtpClient(host, 587);
             MailMessage msg = new MailMessage();
             msg.From = new MailAddress(fromAddr);
             msg.Subject = subject;
             msg.IsBodyHtml = true;
             string emailBody = "Hello <strong>World</strong>";
             msg.Body = HttpUtility.HtmlEncode(emailBody);
             string toAddrList = "toaddress";
             foreach (string addr in toAddrList.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
             {
                 msg.To.Add(addr.Trim());
             }
             System.Net.NetworkCredential credential = new System.Net.NetworkCredential("email", "pass", ""); 
             client.UseDefaultCredentials = false;
             client.Credentials = credential; 
             client.EnableSsl = true; 
             client.Send(msg);

The emails are being sent but html tags are not being render, emails are received with simple text like ![81855-image.png][1] But if I remove System.Web.HttpUtility.HtmlEncode method and pass the emailBody as it is, then it renders html tags properly in email. Can someone please tell me how can I make it work so that I dont have to remove the System.Web.HttpUtility.HtmlEncode method and keeping html tags as well. Thanks

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered TimonYang-MSFT commented

Maybe use msg.Body = System.Web.HttpUtility.HtmlDecode( some_encoded_string ) if you have to send encoded strings.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

there might be some special characters in the body that is why I'm using the HtmlEncode(), using HtmlDecode() will send all those special characters as it is and the email will be rejected from mail server

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 MuhammadTaqi-6635 ·

Then you should build a valid HTML. For example:

 string n = "John & Emma";
 msg.Body = $"Hello, <strong>{HttpUtility.HtmlEncode(n)}</strong>";

0 Votes 0 ·

there might be some special characters in the body that is why I'm using the HtmlEncode(), using HtmlDecode() will send all those special characters as it is and the email will be rejected from mail server

0 Votes 0 ·

@MuhammadTaqi-6635
Was the solution mentioned in Viorel-1's comment useful to you?
I think this should be a viable solution, encoding special characters instead of the entire string.
If it doesn't work, could you please tell us which special characters you are using?
By the way, please select Code Sample when adding code so that we can see it more clearly.
82939-capture.png

0 Votes 0 ·
capture.png (6.1 KiB)