1

我必须发送带有附件的邮件。我的代码仅适用于小于 4mb 的文件。我已经检查了网上的所有内容,但每个人都建议相同的解决方案。那就是更改我已经完成的 webconfig 中的 httpruntime 属性。

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

我已经更改了 Web 配置中具有“超时”属性的所有内容。还在 IIS 中的应用程序配置中更改了 KeepAlive,但即使做了所有这些更改,问题仍然存在于我的应用程序中。每次我尝试上传大于 4mb 的文件时正好 1.5 分钟后连接超时。

点击事件中的代码

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();
            }

        }
4

5 回答 5

1

它具有上传文件的默认限制,请参阅此链接以解决此问题

http://frazsundal.blogspot.com/2011/02/request-filtering-module-is-configured.html

于 2012-04-24T07:32:18.823 回答
1

在你的web.config

添加这一行

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

使您的maxRequestLength="10000"应用程序上传最大大小为 10mb。

于 2012-04-24T08:40:00.177 回答
1

我在我的应用程序中找到了罪魁祸首。这是 smtp 客户端的超时属性,它在大约 1.5 分钟后停止了进程。该属性的默认值是 100 秒,我将其更改为 1500 秒(1500000 毫秒,因为该属性以毫秒为单位)和已成功发送附件。

smtp.Timeout = 1500000;

看到这个了解更多信息。

顺便说一句,我用21mb附件对其进行了测试。

于 2012-04-25T09:18:38.543 回答
0

尝试使用 httpRuntime 节点的 maxRequestLength。可能还需要更改 executionTimeout。

于 2012-04-24T07:41:09.380 回答
0

这听起来与此问题SmtpClient.Send 附件最大大小相似,因此它可能是您的 SMTP 服务器的问题 - 尝试检查设置。

于 2012-04-24T12:23:15.900 回答