System.Net.Mail: The specified string is not in the form required for a subject

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“.

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 your have either a carriage return or line feed in there.

So, obviously, the solution is:

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!

About these ads

23 Responses to System.Net.Mail: The specified string is not in the form required for a subject

  1. Tom Gilkison says:

    Thank you for taking the time to post the solution. If everyone did this, it would be much easier to do what already should have been done.

  2. davidabrahams says:

    Thanks Duncan very helpful and well written post. I was able to fix the problem I was having

  3. sr216viper says:

    I ran into the same problem today. Fortunately for me your web site was the first one that I looked at. Thanks so much for taking the time to post this.

  4. Pingback: Attenzione ad usare la mail come Logger - Framework 4.0

  5. bouquet says:

    Good post!
    Actually, it’s better also to check subject on empty string to prevent ArgumentException on ‘replace’ function.

  6. Sami says:

    Thank you for taking the time to post the solution. If everyone did this, it would be much easier to do what already should have been done.

  7. Manny says:

    Very helpful post, Thank you!
    It saved me a lot of time searching for it. Found on 3rd posting I read.

  8. Tom Phillips says:

    I recently came across this problem and I can validate that what you have posted is exactly what is happening.

    This leads to a big problem though. According to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt, section 2. Syntax of encoded-words), if a subject line is longer than 75 characters, it should be broken up into multiple encoded words. Each encoded word should be separated by CRLF SPACE. If this is not done, then it is possible that the subject line will be clipped.

    How are we supposed to get around this limitation and conform to the specification? I can’t wrap my head around this. Any help would be greatly appreciated!

  9. Julio says:

    Thank you for you post. It was very accurate and the right solution to the problem.

  10. Cynkan says:

    Big thanks for sharing this, you made my day :-)

  11. David says:

    Thanks! You saved me alot of time! :)

  12. Prakash says:

    Thank U Dunkan,

    It took me more than 6 hrs to land on u’r page for solutions, it’s really helpful.

    thanks

  13. replica pens says:

    mont blanc pens south africa,mont blanc south africa,mont blanc pens for sale, mont blanc pens cape town,www.glamourshops.com cheap mont blanc pens,mont blanc pens prices.

  14. I found a very similar write-up to this one on a different blog site, but I like your opinion on it a lot better. Thanks for the added perspective.

  15. Felix says:

    It helped me a lot, Thank you

  16. Ales says:

    Huh. This saved my day. Thanx!!!

  17. Roman says:

    Thanks man! You saved my day!

  18. Pedro says:

    Nice, but how do I insert a newline?

  19. Basbomm says:

    Thank you! Annoying none of this is mentioned in the description of MailMessage.Subject on MSDN.

  20. Aniket Betkikar says:

    Very helpful post, Thank you so much!

Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: