2011-08-22 12 views
35

ho aggiungere un allegato in questo modo:Set nome email Allegato in C#

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

Ma io voglio farlo attaccare con un nome diverso, il nome del file reale è molto lunga e confusa mi piacerebbe allegare come "file.txt", c'è un modo semplice per farlo senza dover fare una copia del file?

risposta

61

ne dite:

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

Questo è ciò che ha funzionato per me: attachment.ContentDisposition.FileName = "file.txt"; – MarkHoward02

5

È necessario caricare l'allegato da un ruscello e poi si può dare un nome e un tipo di supporto.

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