在C#中设置电子邮件附件名称
我添加这样的附件:
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentPath); msg.Attachments.Add(attachment);
但我想把它作为一个不同的名称附加,实际的文件名非常长,令人困惑,我想它附加为“file.txt”,是否有一个简单的方法来做到这一点,而不必做一个副本文件?
怎么样:
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentPath); attachment.Name = "file.txt"; // set name here msg.Attachments.Add(attachment);
您需要从stream中加载附件,然后您可以为其指定名称和媒体types。
var fs = new FileStream("attachmentPath", FileMode.Open); var attachment = new System.Net.Mail.Attachment(fs, "MyAttachmentName.txt", "text/text");