Error when emailing password protected zip

Jul 16, 2009 at 11:27 AM

Hi,

I'm new with the dotnetzip, it has been very easy and helpful. But I've encountered a problem and I'm not sure if it's related to the dotnetzip, to net.mail or I'm missing something.

The thing is I email a weekly report in an Excel sheet, for security reasons now it has to be encrypted and protected with a password.

This is how I do it:

 

    Dim objZIP As ZipFile = New ZipFile()
Dim ms As MemoryStream = New MemoryStream
objZIP.Password = "PASSWORD!"
objZIP.Encryption = EncryptionAlgorithm.WinZipAes256
objZIP.AddEntry("newfile.xls", "", strXLSReport)
objZIP.Save(ms)
ms.Seek(0, SeekOrigin.Begin)
' ... Dim emailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage
' ... Dim ExcelZip As New System.Net.Mail.Attachment(ms, "attachZip.zip", Net.Mime.MediaTypeNames.Application.Zip)
emailMessage.Attachments.Add(ExcelZip)
objSMTPServer.Send(emailMessage)

The email that I receive when executing this doesn't contain anything, and when I say anything I don't only mean the attachment, also recipient, subject, sender and body dissapear.

If I remove the password assignment, it works perfect. I receive the email with all data and the attachment.

I've also tried to save the protected zip to a folder (this worked perfect) and then attach and email it (in this case, the result was the same as using memorystream).

I've made other tests such as changing encryption or the type of the zipped file, but all had the same ending.

Any help or tips will be appreciated, thanks in advance!

 

 

Coordinator
Jul 16, 2009 at 8:19 PM

You might try employing a Using clause or two.  The streams often do not close properly when you do not surround them in Using.

    Dim ms As MemoryStream = New MemoryStream
    Using objZIP As ZipFile = New ZipFile()
      objZIP.Password = "PASSWORD!"
      objZIP.Encryption = EncryptionAlgorithm.WinZipAes256
      objZIP.AddEntry("newfile.xls", "", strXLSReport)
      objZIP.Save(ms)
    End Using

    ms.Seek(0, SeekOrigin.Begin)   ' ...

    Dim emailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage   ' ...

    Dim ExcelZip As New System.Net.Mail.Attachment(ms, "attachZip.zip", Net.Mime.MediaTypeNames.Application.Zip)
    emailMessage.Attachments.Add(ExcelZip)
    objSMTPServer.Send(emailMessage)

have you looked at the result of the System.Mail.SmtpClient.Send ? I think there is a status code in there.

Jul 20, 2009 at 10:55 AM

Hi, the problem has been solved.

I had to change the SMTP Server due to environment requirements, and this seemed to be the main problem, it will need further investigation, but that's on my side.

With the different server the email is received correctly with password and encryption and everything.

Thanks for your the help and quick reply

Coordinator
Jul 20, 2009 at 6:28 PM

glad to hear it.