An invalid character was found in the mail header

31. July 2009

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(); 
} 

short link to this post:

asp.net, debugging , ,

Comments

Mohamed Hachem
Mohamed Hachem
3/2/2010 10:22:35 AM #
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 :)

Cheers

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading