48

I add an attachment like this:

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

But I want to make it attach as a different name, the actual file name is very long and confusing I would like it to attach as "file.txt", is there an easy way to do this without having to make a copy of the file?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

3 Answers3

84

How about:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentPath);
attachment.Name = "file.txt";  // set name here
msg.Attachments.Add(attachment);
Kjellski
  • 925
  • 10
  • 24
Kev
  • 118,037
  • 53
  • 300
  • 385
7

You need to load the attachment from a stream and then you can give it a name and a media type.

var fs = new FileStream("attachmentPath", FileMode.Open);
var attachment = new System.Net.Mail.Attachment(fs, "MyAttachmentName.txt", "text/text");
John Oxley
  • 14,698
  • 18
  • 53
  • 78
0
var attachment = new Attachment(path){
    Name = "File Name"
};
msg.Attachments.Add(attachment);

So what I did was basically to create the attachment and add the name in the initialization.

Kazeem Quadri
  • 247
  • 3
  • 5