2014-09-24 81 views
1

我在ASP.NET MVC上做了一些代碼。我需要發送附件的電子郵件。但附件名稱爲空文件名爲空的郵件附件

這是我的代碼。 請注意,我不需要將上傳的文件保存在服務器中。

馬託參數:

List<HttpPostedFileBase> ScreenshotsOfIssueFiles 

代碼:

List<Attachment> screenshotAttachments = new List<Attachment>(); 
foreach (var files in ScreenshotsOfIssueFiles) 
{ 
    if (files != null && files.ContentLength > 0) 
    { 
     var attachment = new Attachment(files.InputStream, MediaTypeNames.Application.Octet); 
     ContentDisposition disposition = attachment.ContentDisposition; 
     disposition.FileName = Path.GetFileName(files.FileName); 
     disposition.DispositionType = DispositionTypeNames.Attachment; 
     screenshotAttachments.Add(attachment); 
    } 
} 

回答

1

嘗試使用

var attachment = new Attachment(files.InputStream, files.FileName, MediaTypeNames.Application.Octet); 

然後你的代碼是什麼樣子。編輯

List<Attachment> screenshotAttachments = new List<Attachment>(); 
    foreach (var files in ScreenshotsOfIssueFiles) 
    { 
     if (files != null && files.ContentLength > 0) 
     { 
      var attachment = new Attachment(files.InputStream, files.FileName, MediaTypeNames.Application.Octet); 
      ContentDisposition disposition = attachment.ContentDisposition; 
      disposition.FileName = files.FileName; 
      disposition.DispositionType = DispositionTypeNames.Attachment; 
      screenshotAttachments.Add(attachment); 
+0

它不工作仍然。我調試了代碼,發現:ContentDisposition.FileName = null和ContentDisposition.size = -1 – 2014-09-24 12:54:56

+0

你檢查了你在files.FileName上得到了什麼嗎? – DSR 2014-09-24 12:55:38

+0

是的,它給出了文件的名稱。 – 2014-09-24 12:59:50

相關問題