Set Email Attachment name in C#

最后都变了- 提交于 2019-12-09 07:25:05

问题


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?


回答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);



回答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");


来源:https://stackoverflow.com/questions/7149148/set-email-attachment-name-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!