C#开始一个新的MailTo处理HTML和URL编码 [英] C# Start a new MailTo Process and HTML URL Encoding

查看:266
本文介绍了C#开始一个新的MailTo处理HTML和URL编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了Process类的一个新的MailTo扩展方法,这正好填补了新的ProcessStartInfo包含所需的mailto参数的过程。我创建了一个名为FormatMailToArgument(右末)方法,控制字符转换为自己的URL编码equivelants并测试了这一点,它的工作原理,但有这样做的更好的办法?



  ///<总结> 
///<见CREF =过程/>扩展方法。
///< /总结>
公共静态类处理
{
#地区的MailTo

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///&下; PARAM NAME =MAILMESSAGE方式>邮件消息&下; /参数>
公共静态无效的MailTo(这个过程过程中,MAILMESSAGE MAILMESSAGE)
{
的MailTo(
过程中,
mailMessage.To.ToDelimetedString(),
MAILMESSAGE .CC.ToDelimetedString(),
mailMessage.Bcc.ToDelimetedString(),
mailMessage.Subject,
mailMessage.Body);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到>向电子邮件地址< /参数>
公共静态无效的MailTo(这个过程过程中,IEnumerable的<串>至)
{
的MailTo(
过程中,
to.ToDelimetedString(Character.SemiColon),
空,
空,
空,
NULL);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到>向电子邮件地址< /参数>
///< PARAM NAME =主题>将电子邮件受试对象; /参数>
公共静态无效的MailTo(这个过程过程中,IEnumerable的<串>至,串主题)
{
的MailTo(
过程中,
to.ToDelimetedString(Character.SemiColon ),
空,
空,
主题,
NULL);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到>向电子邮件地址< /参数>
///< PARAM NAME =主题>将电子邮件受试对象; /参数>
///< PARAM NAME =身体方式>电子邮件正文< /参数>
公共静态无效的MailTo(
这个过程过程中,
的IEnumerable<串>至,
线主体,
绳体)
{
的MailTo(
过程中,
to.ToDelimetedString(Character.SemiColon),
空,
空,
主题,
体);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到>向电子邮件地址< /参数>
///< PARAM NAME =CC>将抄送电子邮件地址和LT; /参数>
///< PARAM NAME =主题>将电子邮件受试对象; /参数>
///< PARAM NAME =身体方式>电子邮件正文< /参数>
公共静态无效的MailTo(
这个过程过程中,
的IEnumerable<串>至,
的IEnumerable<串> CC,
线主体,
绳体)
{
的MailTo(
过程中,
to.ToDelimetedString(Character.SemiColon),
cc.ToDelimetedString(Character.SemiColon),
空,
主题,
体);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到>向电子邮件地址< /参数>
///< PARAM NAME =CC>将抄送电子邮件地址和LT; /参数>
///< PARAM NAME =密件抄送方式>的密件抄送电子邮件地址< /参数>
///< PARAM NAME =主题>将电子邮件受试对象; /参数>
///< PARAM NAME =身体方式>电子邮件正文< /参数>
公共静态无效的MailTo(
这个过程过程中,
的IEnumerable<串>至,
的IEnumerable<串> CC,
的IEnumerable<串> BCC,
串主题,
绳体)
{
的MailTo(
过程中,
(至== NULL)空:to.ToDelimetedString(Character.SemiColon)
(CC == NULL)空:cc.ToDelimetedString(Character.SemiColon),
(BCC == NULL)空:bcc.ToDelimetedString(Character.SemiColon),
主题,
体);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到>向电子邮件地址< /参数>
///< PARAM NAME =CC>将抄送电子邮件地址和LT; /参数>
///< PARAM NAME =密件抄送方式>的密件抄送电子邮件地址< /参数>
///< PARAM NAME =主题>将电子邮件受试对象; /参数>
///< PARAM NAME =身体方式>电子邮件正文< /参数>
///< PARAM NAME =attachmentPath>该附件的文件路径和LT; /参数>
公共静态无效的MailTo(
这个过程过程中,
的IEnumerable<串>至,
的IEnumerable<串> CC,
的IEnumerable<串> BCC,
串主题,
绳体,
串attachmentPath)
{
的MailTo(
过程中,
(至== NULL)空:到。 ToDelimetedString(Character.SemiColon),
(CC == NULL)空:cc.ToDelimetedString(Character.SemiColon),
(BCC == NULL)空:bcc.ToDelimetedString(Character.SemiColon) ,
主题,
身上,
attachmentPath);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到方式>通过电子邮件发送一个分号delimeted地址< /参数>
///< PARAM NAME =CC>由分号delimeted抄送电子邮件地址< /参数>
///< PARAM NAME =密件抄送>由分号delimeted密件抄送电子邮件地址< /参数>
///< PARAM NAME =主题>将电子邮件受试对象; /参数>
///< PARAM NAME =身体方式>电子邮件正文< /参数>
公共静态无效的MailTo(这个过程过程中,字符串,字符串抄送,字符串密件抄送,字符串主题,绳体)
{
的MailTo(过程中,要,抄送,密送,主题,正文, 空值);
}

///<总结>
///填充进程的mailto<见CREF =的ProcessStartInfo/取代。你可以将任何
///参数为< C>空< / c取代;如果没有必要的。
///< /总结>
///< PARAM NAME =过程>该处理< /参数>
///< PARAM NAME =到方式>通过电子邮件发送一个分号delimeted地址< /参数>
///< PARAM NAME =CC>由分号delimeted抄送电子邮件地址< /参数>
///< PARAM NAME =密件抄送>由分号delimeted密件抄送电子邮件地址< /参数>
///< PARAM NAME =主题>将电子邮件受试对象; /参数>
///< PARAM NAME =身体方式>电子邮件正文< /参数>
///&下; PARAM NAME =attachmentPath>该附件文件路径。注意:这不会在某些
工作///电子邮件应用程序< /参数方式>
公共静态无效的MailTo(
这个过程过程中,
字符串,
弦毫升,
串BCC,
线主体,
串身体,
串attachmentPath)
{
StringBuilder的StringBuilder的=新的StringBuilder();
stringBuilder.Append(Uri.UriSchemeMailto + Character.Colon);
stringBuilder.Append(FormatMailToArgument(到));

如果(!string.IsNullOrEmpty(CC)||!string.IsNullOrEmpty(BCC)||
!string.IsNullOrEmpty(主题)||!string.IsNullOrEmpty(体)||
string.IsNullOrEmpty(attachmentPath))
{
stringBuilder.Append(Character.Question)!;

名单,LT;字符串>参数=新的List<串GT;(); (!string.IsNullOrEmpty(如有))

如果
{
arguments.Add(主体=+ FormatMailToArgument(主题));
}

如果
{
arguments.Add(身体=+ FormatMailToArgument(体))(string.IsNullOrEmpty(身体)!);
}

如果
{
arguments.Add(CC =+ FormatMailToArgument(CC))(string.IsNullOrEmpty(CC)!);
}

如果(string.IsNullOrEmpty(BCC)!)
{
arguments.Add(BCC =+ FormatMailToArgument(BCC));
}

如果
{
arguments.Add(附件=+ FormatMailToArgument(attachmentPath))(string.IsNullOrEmpty(attachmentPath)!);
}

stringBuilder.Append(arguments.ToDelimetedString(Character.Ampersand));
}

process.StartInfo =新的ProcessStartInfo(stringBuilder.ToString());
}

#endregion

#地区的方法

///<总结>
///格式化邮寄地址参数。转换&所述;![CDATA ['%','和;','','','\t','\\\
']]≥他们的
///十六进制表示。
///< /总结>
///< PARAM NAME =参数>该参数< /参数>
///<返回>在格式化参数< /回报>
私人静态字符串FormatMailToArgument(字符串参数)
{
返回参数。
替换(Character.Percent.ToString(),25%)。
替换(Character.Ampersand.ToString(),%26)。
替换(Character.Colon.ToString(),%3A)。
替换(Character.Horizo​​ntalTab.ToString(),%0D)。
替换(Character.NewLine.ToString(),%0A)。
替换(Character.Question.ToString(),%3F)。
替换(Character.Quote.ToString(),%22)。
替换(Character.Space.ToString(),%20);
}

#endregion
}


解决方案

如果你的意思是,有逃避你的电子邮件地址,然后是你可以使用的System.Uri类的更有效的方法。

 字符串escapedAddress = Uri.EscapeUriString(至mailto:乔blogg's\r\\\
@mail.com);
Console.WriteLine(escapedAddress);



输出:

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

您会注意到我的例子单引号字符没有得到逃脱但那是因为要在电子邮件地址它不是必需的。



至于一般的方法你用过我不明白你为什么会添加一个扩展方法Process类。发送电子邮件,并拥有所有的地址转义。附件,服务器认证等等,等等采取为什么不直接使用系统护理。 Net.Mail 类?


I have created a new MailTo extension method for the Process class which just fills the Process with a new ProcessStartinfo which contains the required mailto arguments. I have created a method called FormatMailToArgument (Right at the end) which converts control characters to their Url Encoded equivelants and have tested this and it works but is there a better way of doing this?

/// <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
}

解决方案

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?

这篇关于C#开始一个新的MailTo处理HTML和URL编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆