【问题标题】:Unicode characters disappear in mail attachment nameUnicode 字符在邮件附件名称中消失
【发布时间】:2014-07-22 07:19:42
【问题描述】:

我开发了一个应用程序,它需要通过电子邮件(在附件中)发送压缩文件。一切都很好,除了一件事:附件名称中的西里尔字母(或其他 unicode)字符消失了。它们在邮件名称和邮件正文中工作正常,但在附件中却不行。

我使用以下代码作为附件:

var attachment = new Attachment(file, MediaTypeNames.Application.Zip);

ContentDisposition disposition = attachment.ContentDisposition;

disposition.CreationDate = File.GetCreationTime(file);
disposition.ModificationDate = File.GetLastWriteTime(file);
disposition.ReadDate = File.GetLastAccessTime(file);
disposition.FileName = Path.GetFileName(file);            
disposition.Size = new FileInfo(file).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;            
message.Attachments.Add(attachment);
smtpClient.Send(message);

并使用 SmtpClient 类发送电子邮件。我尝试了一些在 SO 上找到的解决方法,比如

att.Name = "история-болезни.doc";  // non-english filename    
att.Name = System.Web.HttpUtility.UrlEncode(att.Name, System.Text.Encoding.UTF8);

但一无所获...我的日志显示电子邮件以正确的附件名称发送到服务器,我还尝试手动(不是以编程方式)发送此类电子邮件,而且效果也很好。

那么,我该如何解决这个问题?我找到了一些 .Net 框架的补丁,但这并不好,因为我必须将我的应用程序发布到许多不同的客户端,而且我不想在应用程序正常运行之前在他们的计算机上安装补丁程序。

UPD。

【问题讨论】:

  • 你试过this吗?
  • @OğuzSezer,正如我所提到的,我最好使用没有此类可安装修补程序的解决方法,因为它会使应用程序部署复杂化。我还有其他选择吗?
  • 我想没有其他解决方案,我遇到了同样的问题,这是唯一对我有用的解决方案。如果您发现其他东西,请告诉我:)
  • @OğuzSezer,您尝试过框架升级吗?也许它会解决问题?
  • 不,我仍在使用带有修补程序的 V4.0。我不打算在不久的将来升级我的应用程序的框架版本。

标签: c# email unicode smtp


【解决方案1】:

我的猜测是 System.Net.Mail 的附件文件名编码器损坏了,这意味着在继续使用它的同时可能没有任何方法可以解决这个问题。

不过,您可以做的是使用 MailKit 正确编码附件文件名。它允许您从 System.Net.Mail.MailMessage 转换为 MimeKit.MimeMessage,这应该可以很容易地移植。

编辑:

要在 MimeKit 和 MailKit 中创建相同的消息,代码应如下所示:

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";

var builder = new BodyBuilder ();

// Set the plain-text version of the message text
builder.TextBody = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
";

// Add an attachment
builder.Attachments.Add (file);

var attachment = builder.Attachments[0];

// setting the attachment.FileName will set the Content-Disposition's filename
// parameter as well as the Content-Type's name parameter.
attachment.FileName = "история-болезни.doc";

// FWIW, very few, if any, mail clients actually care about these fields...
// they are optional and can be ignored.
//var disposition = attachment.ContentDisposition;
//disposition.CreationDate = File.GetCreationTime (file);
//disposition.ModificationDate = File.GetLastWriteTime (file);
//disposition.ReadDate = File.GetLastAccessTime (file);
//disposition.Size = new FileInfo (file).Length;

// Now we just need to set the message body and we're done
message.Body = builder.ToMessageBody ();

// Now send via MailKit's SmtpClient
smtpClient.Send (message);

【讨论】:

  • 哇!效果很好!非常感谢,我会将此标记为答案
  • 没问题!我也在努力增加对新国际化电子邮件标准的支持(现在只是添加收尾工作)。
猜你喜欢
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 2011-11-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多