2

I am trying to send mail in Sitecore using Sitecore.MainUtil.SendMail(message). I have made message.IsBodyHtml = true. I debugged the code and found that in the body of message the string is encoded (e.g: &lt;p&gt;Hi,&lt;/p&gt;). When the mail comes in outlook, its coming with HTML tags (e.g: <p>Hi,</p>). How to get the mail without HTML Tags in it?. (The whole purpose of the story is to send mail to user when the sitecore password expires.)

Note: I am using RTE to get the value for message from sitecore.

Details of Environment.

Sitecore.NET 8.1 (rev. 151207).

.NET Framework 4.0.30319.42000


EDIT

Code copied from comment:

string mailBody = settingsItem.Fields[Constants.PasswordExpiryFieldNames.MailB‌​ody].Value.ToString(‌​);
var emailMessage = new MailMessage(from, to, subject, mailBody);
emailMessage.IsBodyHtml = true; 
Sitecore.MainUtil.SendMail(emailMessage);
10
  • 1
    And when you switch to "HTML designer" mode in RTE, do you see &gt; and other encoded bits?
    – Marek Musielak
    Nov 9, 2017 at 12:53
  • 1
    Can you share the code where you construct the message, please. What you are doing is correct, so it must be how you extract the message contents and put them in the message body. Nov 9, 2017 at 13:23
  • In HTML designer mode I am able to see html tags and not the encoded bits
    – Anjo
    Nov 9, 2017 at 13:23
  • @MarkCassidy PFB the code. For getting the string from RTE : string mailBody = settingsItem.Fields[Constants.PasswordExpiryFieldNames.MailBody].Value.ToString(); For sending mail: var emailMessage = new MailMessage(from, to, subject, mailBody); emailMessage.IsBodyHtml = true; Sitecore.MainUtil.SendMail(emailMessage);
    – Anjo
    Nov 9, 2017 at 13:28
  • 2
    Use string mailBody = Sitecore.Web.UI.WebControls.FieldRenderer.Render(settingsItem, Constants.PasswordExpiryFieldNames.MailB‌​ody) and if it's still encoded, it means that what you typed in RTE is encoded. If so, go the RTE and switch to HTML design view and paste your message there.
    – Marek Musielak
    Nov 9, 2017 at 14:17

1 Answer 1

2

You should never use richtextField.Value for obtaining content of a rich text field. Sitecore does a lot of processing (e.g. for link processing, media link protection, etc) in renderField pipeline.

You can execute renderField pipeline from code by using

Sitecore.Web.UI.WebControls.FieldRenderer.Render

method. You need to pass the item and name of the field.

In your case the code will be:

string mailBody = FieldRenderer.Render(settingsIte‌​m, Constants.PasswordExpiryFieldNames.MailB‌​ody)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.