代码改变世界

.net邮件错误 :The specified string is not in the form required for a subject.

2012-11-28 19:52  立雪三尺  阅读(1043)  评论(0编辑  收藏  举报

Having your ASP.NET error handling routine, which sends you emails when an error occurs on your site, itself fail is annoying. Especially when you think you’ve made the code robust enough. Anyway the error handler for one site I work on was failing with ArgumentException: The specified string is not in the form required for a subject“.

===============================================================================

这是由于 System.Net.Mail.MailMessage的subject 调用了内部方法MailBnfHelper.HasCROrLF,而这个方法无法对subject中可能出现的回车或换行符做出正确的反应。

解决方案:完善subject:

message.Subject = subject.Replace('\r', ' ').Replace('\n', ' ');

View Code