January 21, 2014 08:59 by
Scott
Sometimes you’ll see this error on your site and it is really annoying. I post this issue as I see many people on forums experiencing on this issue. This is an error message:
“ArgumentException: The specified string is not in the form required for a subject“.
So what is exactly “the form required for a subject”? Googling for this error message returns a lot of junk and misinformed forum posts. It turns out that setting the Subject on a System.Net.Mail.Message internally calls MailBnfHelper.HasCROrLF (thank you Reflector) which does exactly what it says on the tin. Therefore one forum poster’s solution of subject.Replace("\r\n", " ") isn’t going to work when you have either a carriage returnor line feed in there.
The solution is like this:
message.Subject = subject.Replace('\r', ' ').Replace('\n', ' ');
Personally, I think that the MailMessage should to this for you or at least Microsoft should document what actually constitutes a “form required for a subject” in MSDN or, even better, in the actual error message itself!