Resolving 'The specified string is not in the form required for a subject.'
Asked Answered
I

4

80

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?

Iormina answered 30/8, 2011 at 7:4 Comment(0)
P
135

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.

Palmette answered 30/8, 2011 at 7:8 Comment(4)
Thank you for this. But gotta say -- is there any reason that Microsoft couldn't just say that newlines are not allowed?Chromium
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) 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).Palmette
Further note subject = subject.Replace(Environment.NewLine, " ");Binding
@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...Mouse
S
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.

Stannite answered 26/9, 2012 at 7:56 Comment(5)
Is there any documentation to link to on this?Vachil
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.Stannite
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 :)Vachil
@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.Gellman
I tested this with over 900 characters and it works fineHairworm
B
4

For VB.NET

subject = subject.Replace(vbNewLine, "")
Bawdy answered 23/11, 2012 at 8:3 Comment(1)
I assumed subject = subject.Replace(vbCr, " ").Replace(vbLf, " ")Suprematism
D
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.

Derain answered 13/10, 2017 at 9:7 Comment(1)
Length over 78 chars is not a problem here, \r\n presence is, but nice to have this answer too.Hulbard

© 2022 - 2024 — McMap. All rights reserved.