【问题标题】:Set Email Attachment name in C#在 C# 中设置电子邮件附件名称
【发布时间】:2011-11-01 05:24:23
【问题描述】:

我添加这样的附件:

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

但我想让它附加为一个不同的名称,实际的文件名很长而且令人困惑我希望它附加为“file.txt”,有没有一种简单的方法可以做到这一点而不必制作文件的副本?

【问题讨论】:

    标签: c# email attachment email-attachments


    【解决方案1】:

    怎么样:

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

    【讨论】:

    • 这对我有用:attachment.ContentDisposition.FileName = "file.txt";
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-06-18
      • 2012-04-27
      • 2015-06-26
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2013-06-09
      • 2011-08-20
      • 2012-04-23
      相关资源
      最近更新 更多