Brad Bruce
Brad Bruce

Reputation: 7807

How to set a nice name for an e-mail (System.Net.Mail.SmtpClient) attachment

I'm sending attachments using the System.Net.Mail.SmtpClient in C#.
The attachment names are the same as the name of the file I pass into the attachment constructor

myMail.Attachments.Add(new Attachment(attachmentFileName));

How would I go about setting a "nice" name for the attachment? The names I currently have are basically numeric IDs indicating which occurrence of a report is attached. My users are looking for something more friendly like "results.xls".

Upvotes: 6

Views: 4651

Answers (3)

Paul Alexander
Paul Alexander

Reputation: 32367

Save the attachment to the temp folder with the desired name then attach it. Don't forget to delete it after you send the email so the temp folder doesn't grow too large.

Edit: The accepted answer is much better but I'll leave this here for others that go down the same thought path and see how wrong it is.

Upvotes: 1

Partha Choudhury
Partha Choudhury

Reputation: 534

You can use this constructor for Attachment

Upvotes: 0

gimel
gimel

Reputation: 86362

See Attachment.Name Property:

Gets or sets the MIME content type name value in the content type associated with this attachment.

You can set the Attachment .Name property to anything you like. In the example inside the last link, you could have :

// Create  the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
data.Name = "VeryNiceName.dat"; //(not in original example)
...
message.Attachments.Add(data);

Upvotes: 16

Related Questions