Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SmtpClient to send a file attachment

Tags:

c#

asp.net

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?

like image 819
user2588040 Avatar asked Jul 16 '13 15:07

user2588040


People also ask

What is SmtpClient C#?

SMTP Client in C# and VB.NETThe Simple Mail Transfer Protocol (SMTP) is the only standard protocol for sending mail messages over the Internet. GemBox. Email enables you to work with the SMTP protocol in C# and VB.NET using an SmtpClient class.


1 Answers

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));
...
like image 79
jac Avatar answered Sep 27 '22 18:09

jac