【问题标题】:System.net.mail attachment gets .xml file extensionSystem.net.mail 附件获得 .xml 文件扩展名
【发布时间】:2012-05-04 18:09:18
【问题描述】:

我必须将几个文件邮寄到一个电子邮件地址。这些文件需要具有 .tcx 文件扩展名。这些实际上是 xml 文件(Garmin 自行车日志)。

显然当收件人收到邮件时,附件被命名为 xxxxx.tcx.xml。如何强制邮件程序不更改附件文件名?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;

namespace stravamailer
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var files = Directory.GetFiles("E:\\JurgenS\\Downloads\\allrides");

            for (int i = 0; i < files.Length; i++)
            {
                MailMessage oMail = new MailMessage();
                oMail.Body = "";
                oMail.From = new MailAddress("jurgen@ccccc.be","Jurgen Stillaert");
                oMail.Subject = "";
                oMail.To.Add(new MailAddress("jurgen@cccc.be"));
                Attachment att = new Attachment(files[i]);
                att.Name = Path.GetFileName(files[i]);
                oMail.Attachments.Add(att);

                SmtpClient oSmtp = new SmtpClient("uit.telenet.be");
                oSmtp.Send(oMail);
            }
        }
    }
}

【问题讨论】:

  • 尝试将其发送到另一个电子邮件地址(完全不同的提供商;例如 gmail 或 hotmail),以查看附件是否也被重命名。我有一种感觉是因为接收邮件服务器!

标签: c# asp.net system.net.mail


【解决方案1】:

Attachment 类与 MailMessage 类一起使用。所有消息 包括一个 Body,其中包含消息的内容。此外 到正文,您可能需要发送其他文件。这些被发送 作为附件并表示为附件实例。添加一个 邮件的附件,将其添加到 MailMessage.Attachments 收藏。

附件内容可以是字符串、流或文件名。你可以 使用任何附件指定附件中的内容 构造函数。

附件的 MIME Con​​tent-Type 标头信息是 由 ContentType 属性表示。内容类型标头 指定媒体类型和子类型以及任何相关参数。 使用 ContentType 获取与附件关联的实例。

MIME Con​​tent-Disposition 标头由 ContentDisposition 属性。 Content-Disposition 标头指定 附件的演示和文件时间戳。一种 仅当附件是文件时才发送 Content-Disposition 标头。 使用 ContentDisposition 属性获取关联的实例 带附件。

MIME Con​​tent-Transfer-Encoding 标头由 TransferEncoding 属性。

Here is the source

您的解决方案在此 MSDN 链接中。你应该试试 MIME Con​​tent-Type。

【讨论】:

  • files[i] 是一个字符串:文件路径。没有什么像新的附件(文件路径,文件名)。我确实尝试通过设置 att.Name 来重命名文件。我还尝试将其设置为“myride.tcx”并继续添加 .xml。
  • 谢谢,确实是 contenttype: ContentType ct = new ContentType("application/vnd.garmin.tcx+xml")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2011-03-31
相关资源
最近更新 更多