Solution "The specified string does not match the form required for the object."

I have a class that sends emails (MailMessage), but I get the following error:

"The specified string is not in the form required by the object."

Is there a convenient dandy method for disinfecting strings or do I need to write my own?

+52
c # mailmessage
Aug 30 2018-11-11T00:
source share
5 answers

I personally have not tried, but according to this , you only need:

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

or something similar.

Internally, the MailMessage class will validate the object using:

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

Thus, the only limitation (at the moment) is the presence of CR or LF .

+76
Aug 30 2018-11-11T00:
source share

There is also a limit of 168 characters, so you should check this as well.

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

+10
Sep 26
source share

I tried for .NET 2.0 and 4.5 and it allows you to post longer threads than 168 characters. I used Papercut to check mail.

+4
Nov 20 '13 at 12:08
source share

For VB.NET

 subject = subject.Replace(vbNewLine, "") 
+2
Nov 23
source share

I know this has already been answered, but it goes:

First, you need to crop the subject, and then the account for the maximum length of the object ( What is the limit on the length of the email subject?:

 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 end. Then the substring is used to limit the length of the object.

0
Oct 13 '17 at 9:07 on
source share



All Articles