Line-breaks and carriage returns (\r \n) are invalid in email subject

I received this exception when sending an email using System.Net.Mail.MailMessage:

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

System.ArgumentException: The specified string is not in the form required for a subject.
   at System.Net.Mail.Message.set_Subject(String value)
   at System.Net.Mail.MailMessage.set_Subject(String value)

The subject line of the email I was sending had a carriage return and line break in it so it made sense that this would be an invalid subject line. 

And I was able to confirm that a check is made on the subject line to make sure there are not Carriage Returns or Line Feeds when the MailMessage.Subject property is set, but also that these are the only characters checked for.

To remove the line breaks and carriage returns I just did a simple find and replace:

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



2 Comments

Comments have been disabled for this content.