My problem is that as soon as I try to send attachments via an internal SMTP Server there is an error. The error occurs as soon as more than 150KB are attached. The Limit on the SMTP Server is set to 25MB. When I am testing it on my Dev Machine it all works fine but as soon as it is on the Live-Server there is the problem that the File is not opened and not accessable. I am not using any 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)
The code:
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);
So the problem was in my web.config. I had to add the "requestLengthDiskThreshold="512000"" parameter.
<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" />