C#启动新的MailTo进程和HTML URL编码
我已经为Process类创建了一个新的MailTo扩展方法,该方法只是用一个新的ProcessStartinfo填充流程,其中包含所需的MailTo参数。我已经创建了一个名为FormatMailToArgument(右端)的方法,该方法将控制字符转换为Url编码的等效字符,并对此进行了测试,效果良好,但有更好的方法吗C#启动新的MailTo进程和HTML URL编码,c#,process,url-encoding,mailto,processstartinfo,C#,Process,Url Encoding,Mailto,Processstartinfo,我已经为Process类创建了一个新的MailTo扩展方法,该方法只是用一个新的ProcessStartinfo填充流程,其中包含所需的MailTo参数。我已经创建了一个名为FormatMailToArgument(右端)的方法,该方法将控制字符转换为Url编码的等效字符,并对此进行了测试,效果良好,但有更好的方法吗 /// <summary> /// <see cref="Process"/> extension methods. /// </summary>
/// <summary>
/// <see cref="Process"/> extension methods.
/// </summary>
public static class Processes
{
#region MailTo
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="mailMessage">The mail message.</param>
public static void MailTo(this Process process, MailMessage mailMessage)
{
MailTo(
process,
mailMessage.To.ToDelimetedString(),
mailMessage.CC.ToDelimetedString(),
mailMessage.Bcc.ToDelimetedString(),
mailMessage.Subject,
mailMessage.Body);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses.</param>
public static void MailTo(this Process process, IEnumerable<string> to)
{
MailTo(
process,
to.ToDelimetedString(Character.SemiColon),
null,
null,
null,
null);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses.</param>
/// <param name="subject">The email subject.</param>
public static void MailTo(this Process process, IEnumerable<string> to, string subject)
{
MailTo(
process,
to.ToDelimetedString(Character.SemiColon),
null,
null,
subject,
null);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses.</param>
/// <param name="subject">The email subject.</param>
/// <param name="body">The email body.</param>
public static void MailTo(
this Process process,
IEnumerable<string> to,
string subject,
string body)
{
MailTo(
process,
to.ToDelimetedString(Character.SemiColon),
null,
null,
subject,
body);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses.</param>
/// <param name="cc">The Cc email addresses.</param>
/// <param name="subject">The email subject.</param>
/// <param name="body">The email body.</param>
public static void MailTo(
this Process process,
IEnumerable<string> to,
IEnumerable<string> cc,
string subject,
string body)
{
MailTo(
process,
to.ToDelimetedString(Character.SemiColon),
cc.ToDelimetedString(Character.SemiColon),
null,
subject,
body);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses.</param>
/// <param name="cc">The Cc email addresses.</param>
/// <param name="bcc">The Bcc email addresses.</param>
/// <param name="subject">The email subject.</param>
/// <param name="body">The email body.</param>
public static void MailTo(
this Process process,
IEnumerable<string> to,
IEnumerable<string> cc,
IEnumerable<string> 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);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses.</param>
/// <param name="cc">The Cc email addresses.</param>
/// <param name="bcc">The Bcc email addresses.</param>
/// <param name="subject">The email subject.</param>
/// <param name="body">The email body.</param>
/// <param name="attachmentPath">The attachment file path.</param>
public static void MailTo(
this Process process,
IEnumerable<string> to,
IEnumerable<string> cc,
IEnumerable<string> 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);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses delimeted by a semi-colon.</param>
/// <param name="cc">The Cc email addresses delimeted by a semi-colon.</param>
/// <param name="bcc">The Bcc email addresses delimeted by a semi-colon.</param>
/// <param name="subject">The email subject.</param>
/// <param name="body">The email body.</param>
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);
}
/// <summary>
/// Populates the process with mailto <see cref="ProcessStartInfo"/>. You may leave any
/// argument as <c>null</c> if not needed.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="to">To email addresses delimeted by a semi-colon.</param>
/// <param name="cc">The Cc email addresses delimeted by a semi-colon.</param>
/// <param name="bcc">The Bcc email addresses delimeted by a semi-colon.</param>
/// <param name="subject">The email subject.</param>
/// <param name="body">The email body.</param>
/// <param name="attachmentPath">The attachment file path. Note: this will not work in some
/// email applications.</param>
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<string> arguments = new List<string>();
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
/// <summary>
/// Formats the mailto argument. Converts <![CDATA['%', '&', ' ', '?', '\t', '\n']]> to their
/// hexadecimal representation.
/// </summary>
/// <param name="argument">The argument.</param>
/// <returns>The formatted argument.</returns>
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
}
//
///扩展方法。
///
公共静态类进程
{
#地区邮件
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///邮件信息。
公共静态void MailTo(此进程,MailMessage)
{
邮寄(
过程
mailMessage.To.ToDelimeterdString(),
mailMessage.CC.ToDelimeterdString(),
mailMessage.Bcc.ToDelimeterdString(),
mailMessage.Subject,
mailMessage.Body);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///发送电子邮件地址。
公共静态void MailTo(此进程,IEnumerable to)
{
邮寄(
过程
to.ToDelimeterdString(字符.分号),
无效的
无效的
无效的
无效);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///发送电子邮件地址。
///电子邮件主题。
公共静态void MailTo(此进程进程,IEnumerable to,字符串主题)
{
邮寄(
过程
to.ToDelimeterdString(字符.分号),
无效的
无效的
主题,,
无效);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///发送电子邮件地址。
///电子邮件主题。
///电子邮件正文。
公共静态无效邮件(
这个过程,,
我数不清,
字符串主题,
(弦体)
{
邮寄(
过程
to.ToDelimeterdString(字符.分号),
无效的
无效的
主题,,
身体);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///发送电子邮件地址。
///抄送电子邮件地址。
///电子邮件主题。
///电子邮件正文。
公共静态无效邮件(
这个过程,,
我数不清,
IEnumerable cc,
字符串主题,
(弦体)
{
邮寄(
过程
to.ToDelimeterdString(字符.分号),
抄送.ToDelimeterdString(字符.分号),
无效的
主题,,
身体);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///发送电子邮件地址。
///抄送电子邮件地址。
///密件抄送电子邮件地址。
///电子邮件主题。
///电子邮件正文。
公共静态无效邮件(
这个过程,,
我数不清,
IEnumerable cc,
IEnumerable bcc,
字符串主题,
(弦体)
{
邮寄(
过程
(to==null)?null:to.ToDelimeterdString(字符.分号),
(cc==null)?null:cc.ToDelimeterdString(字符.分号),
(bcc==null)?null:bcc.todelimeterdstring(字符.分号),
主题,,
身体);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///发送电子邮件地址。
///抄送电子邮件地址。
///密件抄送电子邮件地址。
///电子邮件主题。
///电子邮件正文。
///附件文件路径。
公共静态无效邮件(
这个过程,,
我数不清,
IEnumerable cc,
IEnumerable bcc,
字符串主题,
弦体,
字符串附件路径)
{
邮寄(
过程
(to==null)?null:to.ToDelimeterdString(字符.分号),
(cc==null)?null:cc.ToDelimeterdString(字符.分号),
(bcc==null)?null:bcc.todelimeterdstring(字符.分号),
主题,,
身体,
附件路径);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///以分号表示的电子邮件地址。
///抄送电子邮件地址用分号表示。
///密件抄送电子邮件地址以分号表示。
///电子邮件主题。
///电子邮件正文。
public static void MailTo(此进程进程、字符串收件人、字符串抄送、字符串密件抄送、字符串主题、字符串正文)
{
MailTo(进程、收件人、抄送、密件抄送、主题、正文、空);
}
///
///用mailto填充进程。您可以保留任何
///如果不需要,则将参数设置为null。
///
///这个过程。
///以分号表示的电子邮件地址。
///抄送电子邮件地址用分号表示。
///密件抄送电子邮件地址以分号表示。
///电子邮件主题。
///电子邮件正文。
///附件文件路径。注意:这在某些情况下不起作用
///电子邮件应用程序。
公共静态无效邮件(
这个过程,,
串到,
字符串cc,
字符串密件抄送,
字符串主题,
弦体,
字符串附件路径)
{
StringBuilder StringBuilder=新的StringBuilder();
string escapedAddress = Uri.EscapeUriString("mailto:joe blogg's\r\n@mail.com");
Console.WriteLine(escapedAddress);
mailto:joe%20blogg's%0D%0A@mail.com
Uri.EscapeUriString(stringToEscape).Replace("&", "%26");