14

I am using the SmtpClient class to send mail and also attach files. Everything seems to work fine, except that the filename in the email attachment says filestest.docx instead of test.docx. It is by default appending the folder name the file is located under. I would like to see only the actual file name.

msg.Attachments.Add(new Attachment("I:/files/test.docx"));

Any ideas?

Ry-
  • 218,210
  • 55
  • 464
  • 476
user2588040
  • 163
  • 1
  • 2
  • 6

1 Answers1

28

Add a ContentType to your attachment.

System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
contentType.Name = "test.docx";
msg.Attachments.Add(new Attachment("I:/files/test.docx", contentType));
...
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
jac
  • 9,666
  • 2
  • 34
  • 63