Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need your advice on how to create a single zip file which compress multiple files and then send an email with the created file as attached.

I am using DotNetZip library for creating zip files.

I can able send multiple zipped files through email, but I need one zip file for the entire files. Please see the code below,

Thanks in advance!!

What I have tried:

foreach (KeyValuePair<string, byte[]=""> doc in attachmentColl)
{
Attachment attachment;
MemoryStream memoryStream = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry(doc.Key + ".xls", doc.Value);
zip.Save(memoryStream);
memoryStream.Position = 0;
attachment = new Attachment(memoryStream, new ContentType("application/zip")) { Name = doc.Key + ".zip" };
}
message.Attachments.Add(attachment);
}
message.To.Add(toAddress);
message.Body = mailbody;
message.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp.XXXXXXXX.net");
smtpClient.Send(message);
Posted
Updated 2-Dec-16 0:37am
Comments
OriginalGriff 2-Dec-16 6:08am    
And? What happens that you didn't expect, or doesn't happen that you did?
Where are you stuck?
What help do you need?
DPM20 2-Dec-16 6:13am    
Thanks for your reply.

Posted code will generate an email with multiple zip files as attached but, I need single zip file which compress all the files.

1 solution

Quote:
Posted code will generate an email with multiple zip files as attached but, I need single zip file which compress all the files.

So...you need us to tell you how to move the zip file creation outside a loop, and fill it inside?

That's the kind of thing a third week student on his first computer course should be able to work out on his own in a couple of minutes...
C#
using (ZipFile zip = new ZipFile())
    {
    foreach (KeyValuePair<string, byte[]> doc in attachmentColl)
        {
        zip.AddEntry(doc.Key + ".xls", doc.Value);
        }
    zip.Save(memoryStream);
    MemoryStream memoryStream = new MemoryStream();
    memoryStream.Position = 0;
    Attachment attachment = new Attachment(memoryStream, new ContentType("application/zip")) { Name = doc.Key + ".zip" };
    message.Attachments.Add(attachment);
    message.To.Add(toAddress);
    message.Body = mailbody;
    message.IsBodyHtml = true;
    SmtpClient smtpClient = new SmtpClient("smtp.XXXXXXXX.net");
    smtpClient.Send(message);
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900