SmtpContent use UTF-8 encoding in file names

Aug 25, 2014 at 3:11 PM
Hello higty.
I have a problem: when I attach files whose names contain non-latin characters
then in the mail, I get random characters or "???" or spaces instead of text.

Sample: "Новый текстовый документ.txt" convert to ">2K9 B5:AB>2K9 4>:C<5=B.txt"

I'm using Windows 7, which encodes the file names in UTF-8.
As a mail server using Gmail, tried and others.
I tried using System.Net.Mail.SmtpClient - works well.

My code:
var content = new SmtpContent();
//content.LoadFileData(file); //don't work
content.Name = Path.GetFileName(file);
content.LoadData(File.ReadAllBytes(file));
message.Contents.Add(content);
Coordinator
Aug 26, 2014 at 1:18 AM
Hi.

It seems to be caused by mail client parser problem.
HigLabo encode filename by RFC2231 but many client could not parse it correctly.
Gmail and outlook.com could not RFC2231.
Content-Disposition: attachment;
  filename*0*=iso-2022-jp'ja'%1B%24B%24%5B%244%24%5B%242%1B%28B;
  filename*1=.jpeg
GMail, Outlook.com encode filename like below.
Content-Disposition: attachment;    filename="=?ISO-2022-JP?B?GyRCJy8nYCdTJ20nWxsoQiAbJEInZCdWJ1wnYydkJ2AnUydtJ1sbKEIgGyRCJ1UnYCdcJ2UnXidWJ18nZBsoQi50eHQ=?="
This encoding is not correct in RFC.But it is used as old style and many mail client parse it correctly.
I'll add a feature that you can select how to encode filename in next release.
Coordinator
Aug 26, 2014 at 3:59 AM
Hi.

I rapidly modified this bug.
You can get latest version from
https://higlabo.codeplex.com/releases/view/129747

You can set MimeWriter default encoding behavior by below code.
MimeWriter.Default.FileNameEncoding = FileNameEncoding.BestEffort;
MimeWriter.Default.MimeHeaderParameterEncoding = MimeHeaderParameterEncoding.Rfc2047;
But default settings may solve your file name encoding problem.
So, you don't have to do anything.
Only you have to do is to replace dll.


regards.
Aug 26, 2014 at 9:02 AM
Thanks for the quick reply. This hotfix partially helped.
With short file names work well (if the file name less than 16 characters).
Else I see "=?utf-8?Q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9 =D1=82=D0=B5=D0=BA=D1=81=D1=82 =D2=AB=E1=BF="
Settings FileNameEncoding and MimeHeaderParameterEncoding do not affect the final result.
Coordinator
Aug 26, 2014 at 11:29 PM
Hi.

Please try
MimeWriter.Default.HeaderRfc2047Encoding = Rfc2047Encoding.Base64;
It may be solve your problem.
Coordinator
Aug 27, 2014 at 12:12 AM
Hi.

I found this is a bug and fixed.
It cause only Rfc2047Encoding.QuotedPrintable.
Please get latest version from here.
https://higlabo.codeplex.com/releases/view/130224

I hope this solve your problem.

regards.
Marked as answer by MrRobespierre on 8/26/2014 at 7:04 PM
Aug 27, 2014 at 2:05 AM
Thank you very much! Now it works perfectly!