Free Support Forum - aspose.com

Unable to open a attachment file that I've just added from upload

Hi,

I can open existing attached files of incoming emails but when I create an email and then add an attachment from an uploaded file, then I get an error when trying to read the file or save the email to eml.

I upload a pdf file, save it as an attachment :

//Ajout du fichier

Attachment att=new Attachment(FU.PostedFile.InputStream,FU.PostedFile.FileName);

email.Message.Attachments.Add(att);

When I try to open the attached file I get the error "impossible to access a closed file"

MemoryStream f = new MemoryStream();

email.Message.Attachments[i].Save(f);

Do you know why?

System.ObjectDisposedException was unhandled by user code
Message=Impossible d'accéder à un fichier fermé.
Source=mscorlib
ObjectName=""
StackTrace:
à System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
à System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32 bufferOffset)
à System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer, Int32 bufferOffset, Int32 length)
à System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
à Aspose.Network.Mail.AttachmentBase.Save(Stream stream)
à eCRM.EmailGetAttachment.Page_Load(Object sender, EventArgs e) dans c:\inetpub\wwwroot\eCRM\EmailGetAttachment.aspx.cs:ligne 49
à System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
à System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
à System.Web.UI.Control.OnLoad(EventArgs e)
à System.Web.UI.Control.LoadRecursive()
à System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

Enviromatic:

Hi,

I can open existing attached files of incoming emails but when I create an email and then add an attachment from an uploaded file, then I get an error when trying to read the file or save the email to eml.

I upload a pdf file, save it as an attachment :

//Ajout du fichier

Attachment att=new Attachment(FU.PostedFile.InputStream,FU.PostedFile.FileName);

email.Message.Attachments.Add(att);

When I try to open the attached file I get the error "impossible to access a closed file"

MemoryStream f = new MemoryStream();

email.Message.Attachments[i].Save(f);

Do you know why?

System.ObjectDisposedException was unhandled by user code
Message=Impossible d'accéder à un fichier fermé.
Source=mscorlib
ObjectName=""
StackTrace:
à System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
à System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32 bufferOffset)
à System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer, Int32 bufferOffset, Int32 length)
à System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
à Aspose.Network.Mail.AttachmentBase.Save(Stream stream)
à eCRM.EmailGetAttachment.Page_Load(Object sender, EventArgs e) dans c:\inetpub\wwwroot\eCRM\EmailGetAttachment.aspx.cs:ligne 49
à System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
à System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
à System.Web.UI.Control.OnLoad(EventArgs e)
à System.Web.UI.Control.LoadRecursive()
à System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

I don't have the problem if a create an attachment from a file system file

Attachment

att = new Attachment(@"d:\tt.pdf");

Hi,

I am sorry but I could not reproduce this issue at my end. Here is my complete code of VS 2008 web application. The code runs file and message is also saved in the end with the attachment.

if (FileUpload1.HasFile == true)
{
try
{
Attachment att = new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName);
MailMessage message = new MailMessage("from@domain.com", "to@domain.com", “subject”, “body”);
message.Attachments.Add(att);
MemoryStream f = new MemoryStream();
message.Attachments[0].Save(f);
message.Save(@“D:\temp\message.msg”, MailMessageSaveType.OutlookMessageFormat);

Label1.Text = “File uploaded and attachment saved.”;
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}

I,

Thank you for your answer.

I found the solution (witch is not satisfactory) and the problem.

Since ASP.net v2, when you upload a document, there is a maximum size for this uploaded file to stay in memory. If the file size is greater than this maximum size then the file is written to disc as a temp file. This temp file is automatically deleted during postback to realese resources. That is why there is an error "cannot acces to a closed file". Default value is 80kB. This value can be modified using the parameter

requestLengthDiskThresholdof the web.config file.

So if I upload a file witch size is lower than the requestLengthDiskThreshold, add it as an attachment and save the file, there is no problem. But if the file size is greater than requestLengthDiskThreshold then I get the error.

Then, I set the value of the requestLengthDiskThreshold parameter to the same value as maxRequestLength and everything works fine.

The problem is that if a large number of users upload big files at the same time, then the server mays run out of memory because I don't know how to release the memory used by uploaded temp file. Moreover, wath happens if I store my email onject in a session variable and really save it during another aspx page postback. Does the temp file still exist in memory?

So my question is, why does your attachment object waits for a mail.save command to consume the attached file? Isn't it possible that the attachment object really gets the file when the constructor is executed?

Best regards,