发送带附件的电子邮件 C#
Sending e-mail with attachment C#
我需要发送带附件的电子邮件。我正在使用以下代码:
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);
消息收到,附件也有,但文件名类似'=?utf-8?B?dXRHRDBZTFJnOUdBMFlzZzBKelF1TkNoPz0NCiA9P3V0Zi04P0I/TG1S?=\',也没有扩展名(.docx)和文件内容看起来像是采用 Base64 编码。
如何发送带有 .docx 文件并保存其扩展名和名称的电子邮件?
将 Contentype 添加到您的附件中
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);
我找到了解决办法
问题出在文件名中的俄文字母中。没有它一切正常。
我需要发送带附件的电子邮件。我正在使用以下代码:
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);
消息收到,附件也有,但文件名类似'=?utf-8?B?dXRHRDBZTFJnOUdBMFlzZzBKelF1TkNoPz0NCiA9P3V0Zi04P0I/TG1S?=\',也没有扩展名(.docx)和文件内容看起来像是采用 Base64 编码。 如何发送带有 .docx 文件并保存其扩展名和名称的电子邮件?
将 Contentype 添加到您的附件中
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);
我找到了解决办法
问题出在文件名中的俄文字母中。没有它一切正常。