.net, programming

Rename an email attachment before sending in .NET

I recently had the need to send an email with a file attachment from disk. The problem I faced was that the filename was not very user friendly at all. What I didn’t want to do however was rename the file on disk, or create another copy of the file with a more generic name, that could create conflicts with other users in the system.

I was happy to find that you can simply change the attachment’s file name after you create it based on the file on disk. So my original file, 2073B129-6DFF-4088-A785-C9CD8B1AADBF.pdf, could appear as Agreement.pdf to the recipient of the email. Much better, don’t you think?

And here’s how I did it:

Attachment attachment;
attachment = new Attachment("2073B129-6DFF-4088-A785-C9CD8B1AADBF.pdf");
attachment.ContentDisposition.FileName = "Agreement.pdf";

There’s obviously more to the code related to creating the message, etc. But this is what you need if you want to give the attachment a different file name.


Tagged , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *