file.OpenReadStream() Cannot access closed stream error

299 Views Asked by At

I want Website users to submit a file through a Web form. Sometimes multiple files. Then these files should be emailed to a specific set of users. I have created an Email Service that accepts a List attachments.

`

    public class EmailModel
    {
        public EmailAddressModel FromUser { get; private set; }
        public List<EmailAddressModel> ToAddresses { get; private set; }
        public List<EmailAddressModel> CcAddresses { get; private set; }
        public List<EmailAddressModel> BccAddresses { get; private set; }
        public List<IFormFile> Attachments { get; private set; }
        public string Subject { get; private set; }
        public string Body { get; private set; }
        public bool isHTML { get; private set; }

        public EmailModel(EmailAddressModel fromUser, List<EmailAddressModel> toAddresses, List<EmailAddressModel> ccAddresses, List<EmailAddressModel> bccAddresses, List<IFormFile> attachments, string subject, string body, bool isHTML)
        {
            FromUser = fromUser;
            ToAddresses = toAddresses;
            CcAddresses = ccAddresses;
            BccAddresses = bccAddresses;
            Attachments = attachments;
            Subject = subject;
            Body = body;
            this.isHTML = isHTML;
        }
    }

EmailService Prepare Email Attachments.

        private void PrepareEmailAttachments()
        {
            foreach (IFormFile attachment in Email.Attachments)
            {
                if (attachment.Length > 0)
                {
                    var stream = attachment.OpenReadStream();
                    using (MemoryStream mStream = new MemoryStream())
                    {
                            stream.CopyTo(mStream);
                            var data = mStream.ToArray();
                            Builder.Attachments.Add(attachment.FileName, data);
                    }
                }

            }
        }

Test Code Snippet

List<IFormFile> Attachments = new List<IFormFile>();
            using (var stream = File.OpenRead("TestFile\\Test Document.pdf"))
            {
                var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
                {
                    Headers = new HeaderDictionary(),
                    ContentType = "application/pdf"
                };
                Attachments.Add(file);
            }


            EmailModel Email = new EmailModel(
                From,
                To,
                CC,
                BCC,
                Attachments,
                "Test Email",
                "This is a build test",
                true);

`

var stream = attachment.OpenReadStream() I am getting a "Cannot Access a closed stream." `

  Message: 
Test method BA.Common.Tests.SendMail.SendEmail threw exception: 
System.ObjectDisposedException: Cannot access a closed Stream.

  Stack Trace: 
BufferedFileStreamStrategy.Seek(Int64 offset, SeekOrigin origin)
FileStream.set_Position(Int64 value)
ReferenceReadStream.ctor(Stream inner, Int64 offset, Int64 length)
FormFile.OpenReadStream()
EmailService.PrepareEmailAttachments() line 111
EmailService.SendMail() line 43
SendMail.SendEmail() line 58

` I am expecting that the attachment gets loaded into the MimeKit to be emailed. I have confirmed that the file is ready in attachment. But I cannot figure out why I can't open the attachment.

1

There are 1 best solutions below

0
On

One of my good friends answered me via email after seeing my post, but he didn't answer here so I will. It turns out that the error was in my integration test, not my project.

I thought that once I added the file to the (Attachment.Add(file); that I could dispose of the stream. In my test I was closing my stream before loading my EmailModel. Simple fix, but I should have kept working up my stack trace to find the error. Thanks Andrew!