1

So below is the code i'm using. Basically it just loops through an array, adds the files to the zip, then saves the zip to a memory stream and then emails the attachment.

When I look at the item in debug I can see that the zipfile has about 20 megabytes of data. When i receive the attachment it only has about 230 bits of data and there is no content. Any thoughts?

byteCount = byteCount + docs[holder].FileSize;
            if (byteCount > byteLimit)
            {
                //create a new stream and save the stream to the zip file
                System.IO.MemoryStream attachmentstream = new System.IO.MemoryStream();
                zip.Save(attachmentstream);

                //create the attachment and send that attachment to the mail
                Attachment data = new Attachment(attachmentstream, "documentrequest.zip");
                theMailMessage.Attachments.Add(data);

                //send Mail
                SmtpClient theClient = new SmtpClient("mymail");
                theClient.UseDefaultCredentials = false;
                System.Net.NetworkCredential theCredential = new System.Net.NetworkCredential("bytebte", "2323232");
                theClient.Credentials = theCredential;
                theClient.Send(theMailMessage);
                zip = new ZipFile();

                //iterate Document Holder
                holder++;
            }
            else
            {
                //create the stream and add it to the zip file
                //System.IO.MemoryStream stream = new System.IO.MemoryStream(docs[holder].FileData);
                zip.AddEntry("DocId_"+docs[holder].DocumentId+"_"+docs[holder].FileName, docs[holder].FileData);
                holder++;

            }

The issue is here Attachment data = new Attachment(attachmentstream, "documentrequest.zip"); once I look at the attachment it has a size of -1 So whats the proper way to attach this item?

  • 1
    Most likely, your call to `zip.Save` closes the attachment stream, making you lose your data. What Zip library are you using? – Jim Mischel Jun 24 '14 at 18:48
  • The most important piece of software here (`zip.`) is not identified. – H H Jun 24 '14 at 19:22
  • In addition to Jim's answer (even though you've likely put this issue behind you by now; this is more for other readers), you could simply reset the `Stream`'s position. Like so: http://stackoverflow.com/a/2267750/722393. – InteXX Mar 02 '15 at 23:50

1 Answers1

2

I suspect that the call to zip.Save closes the stream after it's written. You'll probably end up having to copy the bytes to an array and then create a new MemoryStream for reading. For example:

//create a new stream and save the stream to the zip file
byte[] streamBytes;
using (var ms = new MemoryStream())
{
    zip.Save(ms);
    // copy the bytes
    streamBytes = ms.ToArray();
}

// create a stream for the attachment
using (var attachmentStream = new MemoryStream(streamBytes))
{
    //create the attachment and send that attachment to the mail
    Attachment data = new Attachment(attachmentstream, "documentrequest.zip");
    theMailMessage.Attachments.Add(data);

    // rest of your code here
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Jim I can see that on the line Attachment data = new Attachment(attachmentstream, "documentrequest.zip"); the attachmentstream is still the correct size but the attachment has a size of -1 –  Jun 24 '14 at 19:02
  • Jim - your recommendation actually closes the stream and then the mail cannot be sent –  Jun 24 '14 at 19:04
  • ignore my above comment about closed stream, but yeah still an invalid file –  Jun 24 '14 at 19:05