Set Email Attachment name in C#

26,277

Solution 1

How about:

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

Solution 2

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");
Share:
26,277

Related videos on Youtube

Tom Gullen
Author by

Tom Gullen

Me Web developer. Website http://www.scirra.com

Updated on July 09, 2022

Comments

  • Tom Gullen
    Tom Gullen almost 2 years

    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?

  • MarkHoward02
    MarkHoward02 almost 7 years
    This is what worked for me: attachment.ContentDisposition.FileName = "file.txt";