2012-04-24 101 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