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

what is the easiest way to *just* the last changeset number from git?
21 hours ago via Silver Bird
@ParasValecha so it is a virus some marketing guy manage to win an award for? ;)
3 days ago via Silver Bird
@ParasValecha none. I think they are one of the worse "viruses" you can get. Thrashing your disk and hogging CPU any chance they get
3 days ago via Silver Bird
...i have gone 5 years without one and been fine
3 days ago via Microsoft
... and provide little value on a day to day basis. Don't go to random content, and don't open stuff you don't understand...
3 days ago via Microsoft
I wholeheartedly believe that virus scanners are one of the worst things to install. they really slow down your computer...
3 days ago via Microsoft
RT @CodeWisdom: "When your hammer is C++, everything begins to look like a thumb." - Steve Haflich #fb
5 days ago via rowi for Windows Phone
Follow me on Twitter