2011-08-22 140 views
35

我添加附件像這樣:設置電子郵件附件名稱

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

但我想讓它附加爲不同的名字,實際的文件名很長,混亂我想它連接作爲「file.txt」,是否有一個簡單的方法來做到這一點,而不必複製文件?

回答

61

如何:

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

這是我工作的東西:attachment.ContentDisposition.FileName =「file.txt」; – MarkHoward02

5

您需要加載從流的連接,然後你可以給它一個名字和一個媒體類型。

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