简体   繁体   中英

C# Zip Saving byte[] into zip, converting to stream, sending mail as a mail attachment - proper way to attachment document

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM