Search code examples
javascriptc#mailto

HTML content is not rendered in an Email


I want to open (not send) an email with a content in HTML format. I'm using mailto of javascript, and I have created a string that contains the html in c#.net, but the mail shows the tags instead of rendering HTML. I guess I am missing a content-type: text/html but how do I put it? or is there a more correct way to do open email with content ?

Here's the c#.net code that gets the html page

  [HttpPost]
    public ActionResult SetMailContent(int entreatyID)
    {
         Entreaties entreaty = db.Entreaties.Where(e => e.ID == entreatyID).FirstOrDefault();
         if (entreaty == null)
         {
             return new HttpStatusCodeResult(HttpStatusCode.NotFound, "File Not Found");
         }

         StringWriter stringWriter = new StringWriter();
         using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
         {
             writer.RenderBeginTag(HtmlTextWriterTag.H1);
             writer.Write(entreaty.OpenDate.ToString("dd/MM/yyyy"));
             writer.RenderEndTag();
         }

         string msg = stringWriter.ToString();
         return Json(new { message = msg});
    }

and javascript code:

window.location = "mailto:mail@gmail.com?body=" + SetMailContent(EntreatyID) + "&subject= " + EntreatyID;

Thank you for your help.


Solution

  • You need to add the HTML content to the DOM, not just display it as a string. You can do this via jQuery for example:

    $("#containerId").append($("your mail content"));