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", " ");



Published 13 January 2010 05:39 AM by Jeff Widmer

Comments

# satyam said on 01 November, 2010 04:50 AM

thanks it worked for me.

# Michael Puckett said on 14 February, 2011 05:13 AM

Thanks, I just had this problem and you've fixed it.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Search

Go

This Blog

News

Syndication