Shivkumar
Shivkumar

Reputation: 1903

Sent Zip file through mail in MVC3

I have use ICSharpCode.SharpZipLib.Zip library for creating Zip file it's working fine, but when i have attach this Zip file to mail attachment, mail is not sent due to attached zip file,

here is code for attaching zip file to mail

System.Net.Mail.Attachment attachment = null;
                try
                {
                    MemoryStream memoryStream = new MemoryStream();
                    attachment = new System.Net.Mail.Attachment(memoryStream, "test.zip");
                }
                catch (Exception e)
                {
                    return false;
                } 

please know me how i can send zip file through mail?.

Upvotes: 0

Views: 577

Answers (2)

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

There could be lot of reasons for the issue,

  1. Check file date & time
  2. Size of the file ( Based on server settings)
  3. Try different formats (rar,7z, etc)

Upvotes: 0

Eric J.
Eric J.

Reputation: 150138

Your code

MemoryStream memoryStream = new MemoryStream();
attachment = new System.Net.Mail.Attachment(memoryStream, "test.zip");

passes a Stream, but that Stream is empty (there's nothing in memoryStream).

If you want to use a MemoryStream, you must read the contents of the ZIP file into memory. You can also use a FileStream if the ZIP is already on disk.

If using a MemoryStream, be sure and set its position 0.

memoryStream.Position = 0;

Depending on how you are using SharpZipLib, you may have access to a ZipOutputStream. If you do, I think you could use that.

Upvotes: 1

Related Questions