我必须发送带有附件的邮件。我的代码仅适用于小于 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();
}
}