2011-12-13 75 views
6

我想獲取上傳的文件作爲附件發送到我的ashx文件中。這裏是我使用的代碼:發送已上傳的文件作爲附件

HttpPostedFile fileupload = context.Request.Files[0]; 

//filename w/o the path 
string file = Path.GetFileName(fileupload.FileName); 

MailMessage message = new MailMessage(); 

//*****useless stuff******** 
message.To.Add("[email protected]"); 
message.Subject = "test"; 
message.From = new MailAddress("[email protected]"); 
message.IsBodyHtml = true; 
message.Body = "testing"; 
//*****useless stuff******** 

//Fault line 
message.Attachments.Add(new Attachment(file, MediaTypeNames.Application.Octet)) 

//Send mail 
SmtpClient smtp = new System.Net.Mail.SmtpClient("xxxx", 25); 
smtp.UseDefaultCredentials = false; 
smtp.Credentials = new NetworkCredential("xxx", "xxxx"); 
smtp.Send(message); 

我能夠發送沒有附件的電子郵件。 我需要先保存文件然後添加到附件中嗎?

+0

您需要先保存文件;看到一個類似的問題,涉及將圖像附加到郵件信息:http://stackoverflow.com/questions/6105904/how-to-send-image-as-attachement-without-saving-it-in-file-system – dash 2011-12-13 22:54:13

+0

Yea得到了這個東西。我無法使用Server.MapPath()。該解決方案是HttpContext.Current.Server.MapPath()。 – nikhil 2011-12-13 23:20:01

回答

2

FileName是客戶端上的文件的名稱,而不是服務器上的文件的名稱。您將需要使用SaveAs或InputStream來獲取附件中的任何內容。

Here is a link到MSDN文檔。

+0

謝謝更新了問題中的代碼。 – nikhil 2011-12-13 23:28:14

3

你可以這樣做:

private void btnSend_Click(object sender,EventArgs e) 
{ 
    MailMessage myMail = new MailMessage(); 
    myMail.To = this.txtTo.Text; 
    myMail.From = "<" + this.txtFromEmail.Text + ">" + this.txtFromName.Text; 
    myMail.Subject = this.txtSubject.Text; 

    myMail.BodyFormat = MailFormat.Html; 
    myMail.Body = this.txtDescription.Text.Replace("\n","<br>"); 

    //*** Files 1 ***// 
    if(this.fiUpload1.HasFile) 
    { 
     this.fiUpload1.SaveAs(Server.MapPath("MyAttach/"+fiUpload1.FileName)); 
     myMail.Attachments.Add(new MailAttachment(Server.MapPath("MyAttach/"+fiUpload1.FileName))); 
    } 

    //*** Files 2 ***// 
    if(this.fiUpload2.HasFile) 
    { 
     this.fiUpload2.SaveAs(Server.MapPath("MyAttach/"+fiUpload2.FileName)); 
     myMail.Attachments.Add(new MailAttachment(Server.MapPath("MyAttach/"+fiUpload2.FileName))); 
    } 


    SmtpMail.Send(myMail); 
    myMail = null; 
    this.pnlForm.Visible = false; 
    this.lblText.Text = "Mail Sending."; 
} 
12

您不需要,也不應該你,附件保存到服務器不必要的。下面是關於如何做到這一點的ASP.NET的WebForms http://www.aspsnippets.com/articles/Attach-files-to-email-without-storing-on-disk-using-ASP.Net-FileUpload-Control.aspx

在C#中的MVC做的一篇文章甚至更好:

 public IEnumerable<HttpPostedFileBase> UploadedFiles { get; set; } 

     var mailMessage = new MailMessage(); 
     // ... To, Subject, Body, etc 

     foreach (var file in UploadedFiles) 
     { 
      if (file != null && file.ContentLength > 0) 
      { 
       try 
       { 
        string fileName = Path.GetFileName(file.FileName); 
        var attachment = new Attachment(file.InputStream, fileName); 
        mailMessage.Attachments.Add(attachment); 
       } 
       catch(Exception) { } 
      } 
     } 
3

在Serj薩根的the footsteps之後,這裏是一個使用web表單的處理程序,但<input type="file" name="upload_your_file" />而不是<asp:FileUpload>控制:

HttpPostedFile file = Request.Files["upload_your_file"]; 
if (file != null && file.ContentLength > 0) 
{ 
    string fileName = Path.GetFileName(file.FileName); 
    var attachment = new Attachment(file.InputStream, fileName); 
    mailMessage.Attachments.Add(attachment); 
} 

此,如果你不需要(或不能添加)在你的表單標籤runat="server"是非常有用的。