简体   繁体   中英

Adding files into Zip and sending them via Mail issues C#

I have created a method that collect every file in a folder and then inserts them into a zip file. The zip file is then sent via mail to a customer. My problem is that I can send a mail message with the zip file attached without any problems, But if another user generates the zip file and sends it, it will look like this.

在此处输入图片说明

I had the same issue when trying to attach excel files to a mail message. But i found out that you have to set a ContentType of the files you attach. And after adding this line: ContentType ct = new ContentType("application/vnd.ms-excel"); for the excel files it worked.

My problem is that i am trying to attach a zip file now, i am using this line: `ContentType ct = new ContentType("application/zip"); but this doesnt work. I am using DotNetZipLib-DevKit-v1.9 to add files into a zip file.

this is my code:

public void SendMailedFilesVallensbaek()
        {
            string[] vallensbeakFileNames = Directory.GetFiles(vallensbaekFiles);
            if (vallensbeakFileNames.Count() > 0)
            {
                ContentType ct = new ContentType("application/zip");
                string zipFile = vallensbaekFiles+@"\someZipFile.zip";
                using (ZipFile zip = new ZipFile())
                {
                    foreach (string file in vallensbeakFileNames)
                    {
                            zip.AddFile(file);
                    }

                    zip.Save(zipFile);
                }
                using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("ares"))
                {
                    using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
                    {
                        msg.From = new MailAddress("system@mail.dk");
                        msg.To.Add(new MailAddress("lmy@mail.dk "));
                        msg.Subject = "IBM PUDO";
                        msg.Body = "Best Regards";
                        msg.Body += "<br/>";
                        msg.Body += "me";
                        msg.IsBodyHtml = true;

                        Attachment attachment = new Attachment(zipFile, ct);
                        msg.Attachments.Add(attachment);
                        //foreach (string file in sentFiles)
                        //{
                        //    Attachment attachment = new Attachment(file, ct);
                        //    msg.Attachments.Add(attachment);
                        //}

                        client.Send(msg);
                        client.Dispose();
                        msg.Dispose();
                        ct = null;
                    }
                }
            }
        }

您应该检查zip文件是否在本地文件夹中创建,并且本地文件夹是否可以被ASP.NET用户访问。

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