Attachment order sent by SmtpClient is incorrect when using 'Name' property
.net, c#, smtpclient
Solution
This is because you require a new instance of ContentType for each attachment.
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";
Should fix your issue.
Problem
Can anyone recreate this issue? It seems to me like a quite serious bug in SmtpClient (.NET 4.0) but I can't believe no one has seen this before and Google doesn't seem to show anyone seeing a similar issue. When sending an email with more than 1 attachment and the 'Attachment.Name' property is used, the attachments will have the incorrect names (e.g. 2 attachments will have their names swapped). The work around (and actually probably the correct property to set) is to use ContentDisposition.FileName. But I would be very interested if this happens for everyone. Can anyone recreate this issue? It seems to me like a quite serious bug in SmtpClient (.NET 4.0) but I can't believe no one has seen this before and Google doesn't seem to show anyone seeing a similar issue. You'll need to create a couple of zip files in c:\tmp\emailin\ ``` 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); } ``` If you now look at the eml file in c:\tmp\emailout\ you will see something like ``` 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-- ``` Note how the Content-Type: and Content-Disposition: file names do not match for each attachment. Am I doing something wrong? Is this a bug that I should log with MS?