mailto link encoding in MVC Razor

﹥>﹥吖頭↗ 提交于 2019-12-06 14:51:03
Lukas Kabrt

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!