前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SmtpClient发送邮件遭遇The specified string is not in the form required for a subject.

SmtpClient发送邮件遭遇The specified string is not in the form required for a subject.

作者头像
张善友
发布2018-01-19 10:56:34
7110
发布2018-01-19 10:56:34
举报
文章被收录于专栏:张善友的专栏张善友的专栏

通过System.Net.Mail发送邮件,出现错误:

System.ArgumentException: The specified string is not in the form required for a subject.    at System.Net.Mail.Message.set_Subject(String value)    at System.Net.Mail.MailMessage.set_Subject(String value)

通过Reflector代码看到 System.Net.Mail.MailMessage 的Suject调用了内部方法MailBnfHelper.HasCROrLF

代码语言:js
复制
internal string Subject
{
    get
    {
        return this.subject;
    }
    set
    {
        if ((value != null) && MailBnfHelper.HasCROrLF(value))
        {
            throw new ArgumentException(SR.GetString("MailSubjectInvalidFormat"));
        }
        this.subject = value;
        if (((this.subject != null) && (this.subjectEncoding == null)) && !MimeBasePart.IsAscii(this.subject, false))
        {
            this.subjectEncoding = Encoding.GetEncoding("utf-8");
        }
    }
}
 

相应的解决方案是采用下述代码

代码语言:js
复制
message.Subject = subject.Replace('\r', ' ').Replace('\n', ' ')

这本来应该是Subject应该处理得事情,却要让我们来处理,这该算是微软的程序员写的臭代码。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-07-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档