C# Start a new MailTo Process and HTML URL Encoding

二次信任 提交于 2019-12-04 20:13:30

If you mean is there are more efficient way to escape your email addresses then yes you could use the System.Uri class.

string escapedAddress = Uri.EscapeUriString("mailto:joe blogg's\r\n@mail.com");
Console.WriteLine(escapedAddress);

Output:

mailto:joe%20blogg's%0D%0A@mail.com

You'll notice in my example the single quote character doesn't get escaped but that's because it's not required to be in email addresses.

As far as the general approach you have used I don't see why you would add an extension method to the Process class. To send an email and have all the address escaping. attachments, server authentication etc. etc. taken care of why not just use the System.Net.Mail classes?

As documented in is this official link, the EscapeUriString method assumes that stringToEscape parameter has no escape sequences in it.

This method is perfect for escaping Uris, but be careful because the target string is a mailto: line that can contain many parameters (not only subject, body, ...), obviously separated each one with some escape characters...like & and ?.

So, in my tests with Windows Store Apps with text taken from @anon comment, that string will not be fully escaped using just Uri.EscapeUriString() method, because it contains the escape sequence &.

You will need an extra manual escape to get the whole string be passed as parameter in a mailto: Uri:

Uri.EscapeUriString(stringToEscape).Replace("&", "%26");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!