代码改变世界

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

  立雪三尺  阅读(1064)  评论(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', ' ');

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
View Code
点击右上角即可分享
微信分享提示