mailto link encoding in MVC Razor

2019-06-02 19:47发布

问题:

I have the following model

public class EmailLinkModel
{
    public string mailbody { get; set; }
    public string emailSubject { get; set; }
    public string emailto { get; set; }        
}

mailbody contains the actual text which is a part of mail body. This text contains a long text [ contains special characters like ? < etc and can contain line breaks spaces etc] I want to create a mailto html tag in the partial view and i tried 3 different approaches and at some point my mailto link not opening the default mail client I think the reason was Html or Url ecnoding [ I am showing the mailbody content in a text area as well here to make sure value is coming correctly] Here is my view

@model RoyaltyDb.Models.EmailLinkModel
@{
    Layout = null;
}
<div class="row ">
    @{
        var formatted_doc_data = Model.mailbody;
        /*
        var formatted_doc_data =HttpUtility.HtmlEncode(Model.mailbody);
          */

      /*  var formatted_doc_data = Model.mailbody.ToString().Trim().Replace("\n", "%0D%0A");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace(" ", "%20");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace(" ", "%20");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace("%", "%25");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace("?", "%3F");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace("/", "%2F");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace(":", "%3A");*/
    }
    <a class="btn btn-primary center" href="mailto:@Model.emailto?Subject=@Model.emailSubject&body=@formatted_doc_data" target="_top">Send Mail</a>
</div>
<hr/>
<div class="row">
    @Html.TextAreaFor(m => m.mailbody, new { style = "width: 100%; height:200px;margin:5px 5px;" })
</div>

How can i do proper encoding to create the mailto Link

回答1:

Try Url.Encode method, as you might have missed some special characters in your code for manual encoding.

<a class="btn btn-primary center"
   href="mailto:@Model.emailto?Subject=@Model.emailSubject&body=@Url.Encode(Model.body)">Send Mail</a>

Also please note, that there might be a limit for the length of the href attribute. See this answer for more details.



回答2:

If you are planning on sending an HTML-formatted document via email (like, say, using your MVC view as the basis for an email), creating a mailto link that other email systems such as Outlook, GMail, Zoho, etc. will render and respond to properly can be tricky.

Here is how I do it: Use Uri.EscapeDataString on each of the pieces of data in the link. This way the colon : after mailto is preserved and not encoded, as are the & and ? characters. When I tried escaping or encoding the entire URL I got very poor results once the receiving email system got finished decoding it.

This may not be explicitly what you are doing with your views but I wasn't sure.

To sum up, your link would look something like this in the View:

<a href="mailto:@Uri.EscapeDataString(Model.emailto)?Subject=@Uri.EscapeDataString(Model.emailSubject)&body=@Uri.EscapeDataString(Model.body)">Send Mail</a>