gpt4 book ai didi

c# - 使用 'Name' 属性时 SmtpClient 发送的附件顺序不正确

转载 作者:行者123 更新时间:2023-11-30 12:14:11 24 4
gpt4 key购买 nike

任何人都可以重现这个问题吗?在我看来,这像是 SmtpClient (.NET 4.0) 中的一个非常严重的错误,但我不敢相信以前没有人见过这个,而且 Google 似乎也没有显示任何人看到过类似的问题。

当发送包含 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 记录的错误吗?

最佳答案

这是因为您需要为每个附件创建一个新的 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";

应该可以解决您的问题。

关于c# - 使用 'Name' 属性时 SmtpClient 发送的附件顺序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10105394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com