2012-05-04 43 views
1

我必須將多個文件郵寄到一個電子郵件地址。這些文件需要具有.tcx文件擴展名。這些實際上是xml文件(Garmin自行車日誌)。System.net.mail附件獲取.xml文件擴展名

顯然,當接收者收到郵件時,附件名爲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("[email protected]","Jurgen Stillaert"); 
       oMail.Subject = ""; 
       oMail.To.Add(new MailAddress("[email protected]")); 
       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); 
      } 
     } 
    } 
} 
+0

嘗試將它發送到另一個電子郵件地址(完全不同的提供商;如Gmail或Hotmail),以查看附件是否也被重命名。我有一種感覺,這是因爲接收郵件服務器! – banging

回答

0

的附件類用於與類MAILMESSAGE。所有消息 都包含一個Body,其中包含消息的內容。除了身體上的 ,您可能還想發送其他文件。這些發送作爲附件發送 並表示爲附件實例。要將一個 附件添加到郵件中,請將其添加到MailMessage.Attachments 集合中。

附件內容可以是字符串,流或文件名。您可以使用任何附件 構造函數指定附件中的內容。

附件的MIME內容類型標題信息是由ContentType屬性表示的 。內容類型標題 指定媒體類型和子類型以及任何相關參數。 使用ContentType獲取與附件關聯的實例。

MIME Con​​tent-Disposition標頭由 ContentDisposition屬性表示。 Content-Disposition標題指定 附件的演示文稿和文件時間戳。 A 僅當附件是文件時纔會發送內容處置標題。 使用ContentDisposition屬性獲取與附件關聯的實例 。

MIME Con​​tent-Transfer-Encoding標頭由 TransferEncoding屬性表示。

Here is the source

你的解決方案是在這個MSDN鏈接。你應該嘗試使用MIME Con​​tent-Type。

+0

files [i]是一個字符串:文件路徑。 沒有什麼像新的Attachement(文件路徑,文件名)。我嘗試通過設置att.Name來重命名文件。我也嘗試將其設置爲「myride.tcx」,並繼續添加.xml。 – JurgenStillaert

+0

Thx,它確實是contenttype:ContentType ct = new ContentType(「application/vnd.garmin.tcx + xml」) – JurgenStillaert