2012-04-24 99 views
1

我必須發送郵件附件。我的代碼只適用於文件小於4MB。 我已經檢查了網上的所有內容,但是所有人都建議使用相同的soolution。即在webconfig中更改我已經完成的httpruntime屬性。無法上傳文件大於4 MB使用文件上傳在asp.net 3.5

<httpRuntime maxRequestLength="10000" executionTimeout="1500" /> 

我已經改變了在網絡「超時」屬性config.Also保持活動的應用程序配置做了更改IIS,但即使這樣做後,這一切都改變了問題依然留在我application.everytime我嘗試一切在1.5分鐘後上傳大於4mb的連接超時文件。

在點擊事件

protected void btnSend_Click(object sender, EventArgs e) 
     { 
      MailMessage msg = new MailMessage(); 
      SmtpClient smtp = new SmtpClient(); 
      string strFrom = txtFrom.Text; 
      string strTo = txtTo.Text; 
      string strSubject= ddlTemplate.SelectedItem.Text.ToString(); 
      string strBody =txtBody.Text; 
      string strCC =txtCC.Text; 
      string strBCC =txtBCC.Text; 
      if (this.fuAttachments.HasFile) 
      { 
       Attachment at = new Attachment(fuAttachments.PostedFile.InputStream,fuAttachments.PostedFile.ContentType); 

       at.ContentDisposition.FileName = this.fuAttachments.FileName; 
       msg.Attachments.Add(at); 


      } 
      smtp.EnableSsl = true; 

      msg.From = new MailAddress(strFrom); 
      msg.To.Add(strTo); 
      msg.Subject = strSubject; 
      msg.Body = strBody; 

      //smtp = new SmtpClient("localhost"); 
      //smtp.UseDefaultCredentials = true; 

      try 
      { 
       smtp.Send(msg); 
      } 
      catch (SmtpException Ex) 
      { 

       throw; 
      } 

      if (msg.Attachments.Count > 0) 
      { 
       //Clear the attachments and delete the sessionid folder from tempFiles 
       msg.Attachments.Dispose(); 
      } 

     } 
+0

您是否增加了執行超時? as 1500ms is 1.5sec – 2012-04-24 10:15:14

+0

that's 1500 seconds not ms .... see [this](http://msdn.microsoft.com/en-us/library/e1f13641.aspx) – vatsal 2012-04-24 10:27:03

+0

噢謝謝我糾正 – 2012-04-24 10:36:11

回答

1

我找到了我的應用程序的罪魁禍首。它是smtp客戶端的超時屬性,約1.5分鐘後停止進程。該屬性的默認值是100秒,我改變爲1500秒(1500000毫秒,因爲此屬性值以毫秒爲單位)併成功郵寄附件。

smtp.Timeout = 1500000; 

有關更多信息,請參閱this

順便說一句,我測試了它與21mb附件。

0

用的httpRuntime節點的maxRequestLength嘗試默認限制規範。可能還需要更改executionTimeout。

+0

已經做到了。...still不上傳 – vatsal 2012-04-24 10:03:04

1

在你的web.config

添加此行

<system.web> 
    <httpRuntime maxRequestLength="10000" /> 
</system.web> 

maxRequestLength="10000"讓你的應用程序上傳最大尺寸爲10MB。

+0

已完成....仍然沒有上傳 – vatsal 2012-04-24 10:03:33

0

聽起來很像這個問題SmtpClient.Send attachment maximum size所以它可能是您的SMTP服務器的問題 - 嘗試檢查設置。

+0

我使用的Gmail郵件服務器有25 MB附件上傳限制(如果我沒有錯)...所以smtp服務器不是一個問題 – vatsal 2012-04-24 13:44:43