In this article I would like to explain how to send the compressed or zip document as an email attachments. You can find the c# source code sample how to send the email with zip format. If you are sending the html attachment or some JavaScript there might be change attachment get corrupted. If you are using the outlook web access (OWA) email client to read the email, there might be change of your html get corrupted because of OWA html filtering option.

Best solution is we can compress the file and attach to email as attachment.  We can find the different approachs to send the email, You can find the below best solution for attachment format.

Sending attachment as PDF format:  You can use ItextSharp library to create the pdf file. You will get the lot of example on how to use the ItextSharp to create the pdf file on .net. I have plan to write one artical on creating the pdf using .net source code.

Sending attachment as zip: You can use opensource library to compress the file and send it as email.  Visit my previous article asp.net file compression

Here I have used C# source code to send the email as attachment. Again I have used opensource DotNetZIP library as compression tool to compress the attachment before sending the email.

MemoryStream memoryStreamOfFile = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("Sample Test.txt", ReadmeText, Encoding.Default);
zip.Password = "Test";
zip.Save(memoryStreamOfFile);
memoryStreamOfFile.Position = 0;
}
Attachment attachment = new Attachment(memoryStreamOfFile, archiveName);
MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
message.CC.Add(new MailAddress("[email protected]"));
message.Subject = "This is my subject";
message.Body = "This is the content";
message.Attachments.Add(attachment);
SmtpClient client = new SmtpClient();
client.Send(message);

In above example Zip.save(memoryStreamOfFile) save the zip file to memorystream format and later memoryStreamOfFile used for email attachment.

Download the full .net c# source code here

You can check my previous articles on sending email and file compression

  1. How to send the email using godaddy hosting account (https://www.cmsstores.com/asp-net-sending-email-using-godaddy-hosting-account//)
  2. Asp.net file compression technique(https://www.cmsstores.com/asp-net-file-compression-using-opensource-librar//)
Get me a cup of Coffee
to write in right spirit
3USD


Similar Topics: