Jul
31
2009

An invalid character was found in the mail header

I had to debug an interesting error yesterday. After reading the message given to me I thought it would be easy straightforward, but after looking at the stack trace, I soon realized it was going to be a little more challenging. My first thought was that while sending out an email, we had an invalid character in the header. I’ll just find it, and write a routine to make sure that only valid characters are passed through.

According to the stack trace this error wasn’t thrown while sending an email, it was thrown while sending a file to the client. No biggie, the dev just called an existing method to make sure the value passed was clean. Granted the message is a little unclear considering we weren’t sending mail, but I can deal with that. Was bothered me more was that there was already a routine in place that was being called to strip invalid characters out, and all tests confirmed that it was working as expected. After a bit of digging I found something I never knew before. According to the RFC 2183 specParameter values longer than 78 characters, or which contain non-ASCII characters, MUST be encoded as specified in [RFC 2184].” It turns out that it is very possible (and even required in some cases) that the file name have non US-ASCII characters. When this was written to the response header the .net framework threw an error. Now that I knew what was causing this an why, it was an easy fix.

public static string GetCleanedFileName(string s) 
{    
    char[] chars = s.ToCharArray(); 
    StringBuilder sb = new StringBuilder(); 

    for (int index = 0; index < chars.Length; index++) 
    { 
        string encodedString = EncodeChar(chars[index]); 
        sb.Append(encodedString); 
    } 

    return sb.ToString(); 
} 

private static string EncodeChar(char chr) 
{ 
    UTF8Encoding encoding = new UTF8Encoding(); 
    StringBuilder sb = new StringBuilder(); 
    byte[] bytes = encoding.GetBytes(chr.ToString()); 

    for (int index = 0; index < bytes.Length; index++) 
    { 
        sb.AppendFormat("%{0}", Convert.ToString(bytes[index], 16)); 
    } 

    return sb.ToString(); 
} 

Comments (1) -

Mohamed Hachem

I had similar problem,  it is all Encoding,  when I tried to attach files with names including French characters. I receive an error
"An invalid character was found in the mail header."

When I looked at the code  there was this line :
attachment.ContentDisposition.FileName = uxd_SendFilePath_FileUpload.FileName;

this property allows the sender to suggest the name to be used to store an e-mail attachment on the recipient's computer. This name is a suggestion only; the receiving system can ignore it. The name must not include path information; any such information is ignored by the receiving computer.

Though it is not UTF8 encoded so if you have file name in spanish, french, german, bosnian, etc you will automatically receive an error.

Solution is very simple :
replace
attachment.ContentDisposition.FileName = FileUpload.FileName;

by

attachment.Name = FileUpload.FileName;
attachment.NameEncoding = Encoding.UTF8;

Hence, the user will be able to upload files with names holding weird characters, viva multiculturalism Smile

Cheers

Add comment




biuquote
Loading


AdSense

Software engineer by hobby and trade. When I am not sitting in front of a computer, you can find me playing with my kids. I am lucky enough to be married to my best friend and high school sweetheart. Life couldn't be better!

 

All content is mine, not my employers

Chronology

Tweets

stumped over a design in my app...:(
1 day ago via Silver Bird
RT @ramseyshow: Blake: US adds 243,000 jobs in January and unemployment rate drops to 8.3%, lowest in three years. (AP)
3 days ago via rowi
RT @jnonis: Amen! Walking with ppl does! RT @stephenNcollins I dare say that boycotting a business in the name of Jesus has never won an ...
3 days ago via rowi
@danctheduck wife and I are big tea drinkers though. That or water is about all we have
3 days ago via WindowsLive
@danctheduck lol. We go through 3 pots/day (pot is 30oz) brew at least 3 times or else It gets pricy :) breville tea maker ftw
3 days ago via WindowsLive
@danctheduck how do u brew your TKY? Usually do mine at 195 for 3:30
3 days ago via rowi
@danctheduck wait...now I see it. Stopping by next time I'm in seattle
3 days ago via rowi
Follow me on Twitter