在C#中设置电子邮件附件名称

时间:2011-08-22 14:40:42

标签: c# email attachment email-attachments

我添加了这样的附件:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentPath);   
msg.Attachments.Add(attachment);   

但是我想把它作为一个不同的名字附加,实际的文件名很长而且令人困惑我想把它作为“file.txt”附加,是否有一个简单的方法来做到这一点,而不必做一个该文件的副本?

2 个答案:

答案 0 :(得分:68)

怎么样:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentPath);
attachment.Name = "file.txt";  // set name here
msg.Attachments.Add(attachment);

答案 1 :(得分:5)

您需要从流中加载附件,然后您可以为其指定名称和媒体类型。

var fs = new FileStream("attachmentPath", FileMode.Open);
var attachment = new System.Net.Mail.Attachment(fs, "MyAttachmentName.txt", "text/text");