C#启动一个新的MailTo进程和HTML URL编码

我为Process类创建了一个新的MailTo扩展方法,它只是用一个包含所需mailto参数的新ProcessStartinfo填充Process。 我已经创建了一个名为FormatMailToArgument的方法(最后在右边),它将控制字符转换为它们的Url编码等价物并测试了它并且它可以工作但是有更好的方法吗?

///  ///  extension methods. ///  public static class Processes { #region MailTo ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// The mail message. public static void MailTo(this Process process, MailMessage mailMessage) { MailTo( process, mailMessage.To.ToDelimetedString(), mailMessage.CC.ToDelimetedString(), mailMessage.Bcc.ToDelimetedString(), mailMessage.Subject, mailMessage.Body); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses. public static void MailTo(this Process process, IEnumerable to) { MailTo( process, to.ToDelimetedString(Character.SemiColon), null, null, null, null); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses. /// The email subject. public static void MailTo(this Process process, IEnumerable to, string subject) { MailTo( process, to.ToDelimetedString(Character.SemiColon), null, null, subject, null); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses. /// The email subject. /// The email body. public static void MailTo( this Process process, IEnumerable to, string subject, string body) { MailTo( process, to.ToDelimetedString(Character.SemiColon), null, null, subject, body); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses. /// The Cc email addresses. /// The email subject. /// The email body. public static void MailTo( this Process process, IEnumerable to, IEnumerable cc, string subject, string body) { MailTo( process, to.ToDelimetedString(Character.SemiColon), cc.ToDelimetedString(Character.SemiColon), null, subject, body); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses. /// The Cc email addresses. /// The Bcc email addresses. /// The email subject. /// The email body. public static void MailTo( this Process process, IEnumerable to, IEnumerable cc, IEnumerable bcc, string subject, string body) { MailTo( process, (to == null) ? null : to.ToDelimetedString(Character.SemiColon), (cc == null) ? null : cc.ToDelimetedString(Character.SemiColon), (bcc == null) ? null : bcc.ToDelimetedString(Character.SemiColon), subject, body); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses. /// The Cc email addresses. /// The Bcc email addresses. /// The email subject. /// The email body. /// The attachment file path. public static void MailTo( this Process process, IEnumerable to, IEnumerable cc, IEnumerable bcc, string subject, string body, string attachmentPath) { MailTo( process, (to == null) ? null : to.ToDelimetedString(Character.SemiColon), (cc == null) ? null : cc.ToDelimetedString(Character.SemiColon), (bcc == null) ? null : bcc.ToDelimetedString(Character.SemiColon), subject, body, attachmentPath); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses delimeted by a semi-colon. /// The Cc email addresses delimeted by a semi-colon. /// The Bcc email addresses delimeted by a semi-colon. /// The email subject. /// The email body. public static void MailTo(this Process process, string to, string cc, string bcc, string subject, string body) { MailTo(process, to, cc, bcc, subject, body, null); } ///  /// Populates the process with mailto . You may leave any /// argument as null if not needed. ///  /// The process. /// To email addresses delimeted by a semi-colon. /// The Cc email addresses delimeted by a semi-colon. /// The Bcc email addresses delimeted by a semi-colon. /// The email subject. /// The email body. /// The attachment file path. Note: this will not work in some /// email applications. public static void MailTo( this Process process, string to, string cc, string bcc, string subject, string body, string attachmentPath) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(Uri.UriSchemeMailto + Character.Colon); stringBuilder.Append(FormatMailToArgument(to)); if (!string.IsNullOrEmpty(cc) || !string.IsNullOrEmpty(bcc) || !string.IsNullOrEmpty(subject) || !string.IsNullOrEmpty(body) || !string.IsNullOrEmpty(attachmentPath)) { stringBuilder.Append(Character.Question); List arguments = new List(); if (!string.IsNullOrEmpty(subject)) { arguments.Add("subject=" + FormatMailToArgument(subject)); } if (!string.IsNullOrEmpty(body)) { arguments.Add("body=" + FormatMailToArgument(body)); } if (!string.IsNullOrEmpty(cc)) { arguments.Add("CC=" + FormatMailToArgument(cc)); } if (!string.IsNullOrEmpty(bcc)) { arguments.Add("BCC=" + FormatMailToArgument(bcc)); } if (!string.IsNullOrEmpty(attachmentPath)) { arguments.Add("attachment=" + FormatMailToArgument(attachmentPath)); } stringBuilder.Append(arguments.ToDelimetedString(Character.Ampersand)); } process.StartInfo = new ProcessStartInfo(stringBuilder.ToString()); } #endregion #region Methods ///  /// Formats the mailto argument. Converts  to their /// hexadecimal representation. ///  /// The argument. /// The formatted argument. private static string FormatMailToArgument(string argument) { return argument. Replace(Character.Percent.ToString(), "%25"). Replace(Character.Ampersand.ToString(), "%26"). Replace(Character.Colon.ToString(), "%3A"). Replace(Character.HorizontalTab.ToString(), "%0D"). Replace(Character.NewLine.ToString(), "%0A"). Replace(Character.Question.ToString(), "%3F"). Replace(Character.Quote.ToString(), "%22"). Replace(Character.Space.ToString(), "%20"); } #endregion } 

如果您的意思是有更有效的方法来转义您的电子邮件地址,那么您可以使用System.Uri类。

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

输出:

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

你会注意到我的例子中单引号字符没有被转义,但这是因为它不需要在电子邮件地址中。

就您使用的一般方法而言,我不明白为什么要向Process类添加扩展方法。 发送电子邮件并让所有地址都逃脱。 附件,服务器身份validation等等。照顾为什么不只是使用System.Net.Mail类?

如此官方链接中所述, EscapeUriString方法假定stringToEscape参数中没有转义序列。

这个方法非常适合转义Uris,但要小心,因为目标字符串是一个mailto:行,可以包含许多参数(不仅是主题,正文,…),显然每个参数都有一些转义字符…就像

因此,在我的Windows Store应用程序测试中,文本取自@anon注释,该字符串不会仅使用Uri.EscapeUriString()方法完全转义,因为它包含转义序列

你将需要一个额外的手动转义来将整个字符串作为参数传递到mailto: Uri:

 Uri.EscapeUriString(stringToEscape).Replace("&", "%26");