77

I have a class that sends an Email (MailMessage) but I get the following error:

"The specified string is not in the form required for a subject."

Is there a handy dandy method to sanitize the strings or do I have to write my own?

Matt
  • 25,943
  • 66
  • 198
  • 303

4 Answers4

132

I haven't personally tried it, but according to this, you only need:

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

or something equivalent.

Internally, the MailMessage class will check the subject with:

if (value != null && MailBnfHelper.HasCROrLF(value)) 
{
   throw new ArgumentException(SR.GetString(SR.MailSubjectInvalidFormat));
}

So the only limitation (for now) happens to be the presence of CR or LF.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • 4
    Thank you for this. But gotta say -- is there any reason that Microsoft couldn't just *say* that newlines are not allowed? – Gerard ONeill Apr 05 '17 at 01:42
  • 2
    You know documentation is hard ;-) The reason it is not allowed is most likely, that CRLF has very special purposes in the message format (see [RFC5322](https://tools.ietf.org/html/rfc5322)) and they didn't wanted to bother automatically changing user's data, but rather fail and let have him do it manually (like shown above). – Christian.K Apr 05 '17 at 04:36
  • 2
    Further note `subject = subject.Replace(Environment.NewLine, " ");` – Nae Feb 20 '20 at 13:34
  • 1
    @Nae, the correct way (judging by the source code of MailMessage) is to replace BOTH `\r` and `\n` characters. Your solution will only work in some cases... – Mladen B. Feb 26 '21 at 15:33
9

Also there is a limit of 168 characters so you should check for that too.

UPDATE: sorry this is complete bullshit :) It must have been a line break in my case.

mike nelson
  • 21,218
  • 14
  • 66
  • 75
  • 2
    Is there any documentation to link to on this? – mlhDev Aug 15 '13 at 21:24
  • I don't think so, we just figured it out by trial and error. I could be wrong, you are welcome to try it yourself and post the result. – mike nelson Aug 16 '13 at 04:24
  • Sounds good to me, I was just curious how you came by the answer (and such an odd value at that). Already +1 - a solution is a solution :) – mlhDev Aug 16 '13 at 14:02
  • @mikenelson - I wonder why there is such a small limit. Imagine if this is used in SSIS (An ETL tool), your package could simply fail because of this limitation. – Steam Jan 09 '14 at 00:47
  • I tested this with over 900 characters and it works fine – Agile Jedi Dec 01 '16 at 17:02
4

For VB.NET

subject = subject.Replace(vbNewLine, "")
Clarice Bouwer
  • 3,631
  • 3
  • 32
  • 55
1

I know this has already answered, but it goes:

First, you need to trim the subject then account for the subject max length (What is the email subject length limit?):

subject = subject.Trim();
subject = subject.Substring(0, Math.Min(subject.Length, 78));

This will remove any new lines or empty spaces at the beginning and the end. Then Substring is used to restrict the subject length.

Spikolynn
  • 4,067
  • 2
  • 37
  • 44
Weggo
  • 138
  • 2
  • 11