Wednesday, September 3, 2008

SmtpClient exception - An invalid character was found in the mail header

This hit me today as I tried to move a configuration section to another configuration file. It seems that the SmtpClient class does not like non-english letters in email header.

MailMessage mail = new MailMessage();
mail.From = new MailAddress( 
    "ąęłóżśńć", // <- the cause of the exception
    Encoding.UTF8 ); 

What's really scary is that the code above actually works - the exception occured only when the subject name was read from the configuration file!

After an intense struggle, I've found the solution. Just add

mail.SubjectEncoding = Encoding.UTF8;
somewhere around. It works, at least for me.


Anonymous said...

Hi Wiktor,

I did what you mentioned and it worked. But I still had the same error message because of using UTF8 characters in "Sender Name" field. Checking the String class methods I found the Normalize() method and I applied it to the "sender name" string and voila...

thank you for you seggestion.

Mohamed Hachem said...

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 :
attachment.ContentDisposition.FileName = FileUpload.FileName;


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

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


Anonymous said...

I had the same problem but only with windows 7 on non english language.
Solutio witch work :
Control panel->Regional and Language->Administrativ tab->Change system local : change in English (usa or uk) -> reboot

INDIAN said...

Hi All, I received this error when if this is modified to "network" --- then error got resolved. ( My understanding is - Incase of specified pickupdirectory option, the header -encoding utf-8 (base64) was giving error )

Hope it helps

Shahid said...

I have the same problem. i did according to your suggestion but its not working for me.

here is my code.
string[] emailAddress = null;
// To List
if (ToList != "" & ToList != string.Empty & ToList != "Error")
emailAddress = (ToList.Replace(";", ",").Trim(new Char[] { ',' })).Split(',');
for (int i = 0; i < emailAddress.Length; i++)
mail.To.Add(new MailAddress(emailAddress[i], emailAddress[i], System.Text.Encoding.UTF8));

Here is my logging information including stack trace.
2015-02-17 18:59:33,849 [13] DEBUG Conference - CommonFunction.SendEmailwithattachment started_4
2015-02-17 18:59:33,849 [13] DEBUG Conference - CommonFunction.SendEmailwithattachment ToList: linda.hammarströ
2015-02-17 18:59:33,849 [13] DEBUG Conference - CommonFunction.SendEmailwithattachment CCList:
2015-02-17 18:59:33,849 [13] DEBUG Conference - CommonFunction.SendEmailwithattachment BCCList:
2015-02-17 18:59:33,849 [13] DEBUG Conference - CommonFunction.SendEmailwithattachment Subject: this is test email from booking system
2015-02-17 18:59:33,849 [13] DEBUG Conference - CommonFunction.SendEmailwithattachment Message: test

2015-02-17 18:59:33,849 [13] ERROR Conference - Exception in CommonFunction.SendEmailwithattachment_4:
at System.Net.Mail.MailAddressParser.ParseLocalPart(String data, Int32& index, Boolean expectAngleBracket, Boolean expectMultipleAddresses)
at System.Net.Mail.MailAddressParser.ParseAddress(String data, Boolean expectMultipleAddresses, Int32& index)
at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
at CommonFunction.SendEmailwithattachment(String ToList, String CCList, String BCCList, String Subject, String MessageBody, String EmailAddress, String Password, String file) in c:\Booking System\App_Code\Data_Layer\basicFucntions\CommonFunction.cs:line 276
2015-02-17 18:59:33,849 [13] ERROR Conference - Shahid: Exception in btnSend_Click:
An invalid character was found in the mail header: 'ö'.