SMTP 服务器不发送带附件的邮件 .NET MVC
SMTP Server does not send Mail with attachments .NET MVC
我的问题是,一旦我尝试通过内部 SMTP 服务器发送附件,就会出现错误。一旦附加超过 150KB,就会发生错误。 SMTP 服务器上的限制设置为 25MB。
当我在我的开发机器上测试它时,它一切正常,但一旦它在 Live-Server 上,就会出现文件未打开且无法访问的问题。我没有使用任何 FileStream。
Exception: System.ObjectDisposedException
Message: Cannot access a closed file.
Source: mscorlib
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
at System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32 bufferOffset)
at System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer, Int32 bufferOffset, Int32 length)
at System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Mime.MimePart.Send(BaseWriter writer, Boolean allowUnicode)
at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer, Boolean allowUnicode)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
代码:
NetworkCredential loginInfo = new NetworkCredential(model.user, model.password);
SmtpClient smtpClient = new SmtpClient(model.server, int.Parse(model.port));
smtpClient.EnableSsl = model.ssl;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(model.from); //From Adress
mail.To.Add(new MailAddress(model.to)); //To Adress
mail.Subject = model.subject;
mail.Body = model.body;
mail.IsBodyHtml = true;
foreach (var file in model.attachments)
{
mail.Attachments.Add(new Attachment(file.InputStream, file.FileName));
}
smtpClient.Send(mail);
所以问题出在我的 web.config 上。
我必须添加“requestLengthDiskThreshold="512000"" 参数。
<httpRuntime targetFramework="4.7.1" maxRequestLength="512000" requestLengthDiskThreshold="512000" executionTimeout="600" enableKernelOutputCache="false" relaxedUrlToFileSystemMapping="true" requestValidationMode="4.0" enableVersionHeader="false" requestValidationType="Sitecore.Web.RequestValidators.SitecoreBackendRequestValidator, Sitecore.Kernel" />
我的问题是,一旦我尝试通过内部 SMTP 服务器发送附件,就会出现错误。一旦附加超过 150KB,就会发生错误。 SMTP 服务器上的限制设置为 25MB。 当我在我的开发机器上测试它时,它一切正常,但一旦它在 Live-Server 上,就会出现文件未打开且无法访问的问题。我没有使用任何 FileStream。
Exception: System.ObjectDisposedException
Message: Cannot access a closed file.
Source: mscorlib
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
at System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32 bufferOffset)
at System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer, Int32 bufferOffset, Int32 length)
at System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Mime.MimePart.Send(BaseWriter writer, Boolean allowUnicode)
at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer, Boolean allowUnicode)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
代码:
NetworkCredential loginInfo = new NetworkCredential(model.user, model.password);
SmtpClient smtpClient = new SmtpClient(model.server, int.Parse(model.port));
smtpClient.EnableSsl = model.ssl;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(model.from); //From Adress
mail.To.Add(new MailAddress(model.to)); //To Adress
mail.Subject = model.subject;
mail.Body = model.body;
mail.IsBodyHtml = true;
foreach (var file in model.attachments)
{
mail.Attachments.Add(new Attachment(file.InputStream, file.FileName));
}
smtpClient.Send(mail);
所以问题出在我的 web.config 上。 我必须添加“requestLengthDiskThreshold="512000"" 参数。
<httpRuntime targetFramework="4.7.1" maxRequestLength="512000" requestLengthDiskThreshold="512000" executionTimeout="600" enableKernelOutputCache="false" relaxedUrlToFileSystemMapping="true" requestValidationMode="4.0" enableVersionHeader="false" requestValidationType="Sitecore.Web.RequestValidators.SitecoreBackendRequestValidator, Sitecore.Kernel" />