Gleb
Gleb

Reputation: 1432

Sending e-mail with attachment C#

I need to send email message with attachment. I'm using below code:

MailMessage msg = new MailMessage("adrFrom", "adrTo", "header", "body");

SmtpClient client = new SmtpClient("hostName", 25);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("accountName", "password");

Attachment atch = new Attachment(filePath, MediaTypeNames.Application.Octet);

atch.Name = "FileName.docx";

msg.Attachments.Add(atch);

client.Send(msg);

Message is received, and attachment is there too, but file name looks like ' =?utf-8?B?dXRHRDBZTFJnOUdBMFlzZzBKelF1TkNoPz0NCiA9P3V0Zi04P0I/TG1S?=\', also there is no extension(.docx) and file content looks like it's in the Base64 encoding. How can I send e-mail message with .docx file saving it extension and name?

Upvotes: 1

Views: 2245

Answers (2)

Gleb
Gleb

Reputation: 1432

I have found a solution

The problem was in Russian letters in the file name. Without it everything works fine.

Upvotes: 0

Dhru 'soni
Dhru 'soni

Reputation: 1058

Add Contentype 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);

Upvotes: 1

Related Questions