在 C# 中设置电子邮件附件名称

发布于 2024-11-30 19:47:40 字数 253 浏览 1 评论 0原文

我添加这样的附件:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentPath);   
msg.Attachments.Add(attachment);   

但我想将其附加为不同的名称,实际文件名非常长且令人困惑我希望将其附加为“file.txt”,有没有一种简单的方法可以做到这一点而不需要必须复制文件吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小姐丶请自重 2024-12-07 19:47:40

怎么样:

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

How about:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentPath);
attachment.Name = "file.txt";  // set name here
msg.Attachments.Add(attachment);
鲜血染红嫁衣 2024-12-07 19:47:40

您需要从流中加载附件,然后可以为其指定名称和媒体类型。

var fs = new FileStream("attachmentPath", FileMode.Open);
var attachment = new System.Net.Mail.Attachment(fs, "MyAttachmentName.txt", "text/text");

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");
浪漫之都 2024-12-07 19:47:40
var attachment = new Attachment(path){
    Name = "File Name"
};
msg.Attachments.Add(attachment);

所以我所做的基本上就是创建附件并在初始化中添加名称。

var attachment = new Attachment(path){
    Name = "File Name"
};
msg.Attachments.Add(attachment);

So what I did was basically to create the attachment and add the name in the initialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文