4

谁能重现这个问题?在我看来,这似乎是 SmtpClient (.NET 4.0) 中的一个非常严重的错误,但我不敢相信以前没有人见过这个问题,而且谷歌似乎没有让任何人看到类似的问题。

当发送包含超过 1 个附件的电子邮件并使用 'Attachment.Name' 属性时,附件的名称将不正确(例如,2 个附件的名称将被交换)。解决方法(实际上可能是要设置的正确属性)是使用 ContentDisposition.FileName。但如果这发生在每个人身上,我会非常感兴趣。谁能重现这个问题?在我看来,这似乎是 SmtpClient (.NET 4.0) 中的一个非常严重的错误,但我不敢相信以前没有人见过这个问题,而且谷歌似乎没有让任何人看到类似的问题。您需要在 c:\tmp\emailin\ 中创建几个 zip 文件

var zipCt = new ContentType { MediaType = MediaTypeNames.Application.Zip };

var attachmentA = new Attachment(@"c:\tmp\emailin\a.zip", zipCt);
attachmentA.ContentDisposition.FileName = "a.zip";
attachmentA.Name = "a.zip";

var attachmentB = new Attachment(@"c:\tmp\emailin\b.zip", zipCt);
attachmentB.ContentDisposition.FileName = "b.zip";
attachmentB.Name = "b.zip";

var msg = new MailMessage("testfrom@example.com", "testto@example.com")
{
       Body = "body",
       Subject = "subject"
};
msg.Attachments.Add(attachmentA);
msg.Attachments.Add(attachmentB);

using (var smtp = new SmtpClient())
{
     smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
     smtp.PickupDirectoryLocation = @"c:\tmp\emailout\";
     smtp.Send(msg);
}

如果您现在查看 c:\tmp\emailout\ 中的 eml 文件,您将看到类似

X-Sender: testfrom@example.com
X-Receiver: testto@example.com
MIME-Version: 1.0
From: testfrom@example.com
To: testto@example.com
Date: 11 Apr 2012 12:36:48 +0100
Subject: subject
Content-Type: multipart/mixed; boundary=--boundary_0_1b7bb1ee-ba28-4258-b662-554adb7ff81a


----boundary_0_1b7bb1ee-ba28-4258-b662-554adb7ff81a
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

body
----boundary_0_1b7bb1ee-ba28-4258-b662-554adb7ff81a
Content-Type: application/zip; name=b.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=a.zip

UEsDBAoAAAAAAG5ki0AAAAAAAAAAAAAAAAAFAAAAYS50eHRQSwECPwAKAAAAAABu
ZItAAAAAAAAAAAAAAAAABQAkAAAAAAAAACAAAAAAAAAAYS50eHQKACAAAAAAAAEA
GADa2JQw1xfNAdrYlDDXF80B2tiUMNcXzQFQSwUGAAAAAAEAAQBXAAAAIwAAAAAA
----boundary_0_1b7bb1ee-ba28-4258-b662-554adb7ff81a
Content-Type: application/zip; name=a.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=b.zip

UEsDBAoAAAAAAHZki0AAAAAAAAAAAAAAAAAFAAAAYi50eHRQSwECPwAKAAAAAAB2
ZItAAAAAAAAAAAAAAAAABQAkAAAAAAAAACAAAAAAAAAAYi50eHQKACAAAAAAAAEA
GAD67/k51xfNAfrv+TnXF80B2tiUMNcXzQFQSwUGAAAAAAEAAQBXAAAAIwAAAAAA
----boundary_0_1b7bb1ee-ba28-4258-b662-554adb7ff81a--

请注意每个附件的 Content-Type: 和 Content-Disposition: 文件名如何不匹配。

难道我做错了什么?这是我应该用 MS 记录的错误吗?

4

1 回答 1

3

这是因为您需要为每个附件创建一个新的 ContentType 实例。

var zipCt = new ContentType { MediaType = MediaTypeNames.Application.Zip };
var zipCt2 = new ContentType { MediaType = MediaTypeNames.Application.Zip };

var attachmentA = new Attachment(@"c:\tmp\emailin\a.zip", zipCt);
attachmentA.ContentDisposition.FileName = "a.zip";
attachmentA.Name = "a.zip";

var attachmentB = new Attachment(@"c:\tmp\emailin\b.zip", zipCt2);
attachmentB.ContentDisposition.FileName = "b.zip";
attachmentB.Name = "b.zip";

应该解决你的问题。

于 2012-04-11T12:15:42.487 回答