简体   繁体   English

将文件添加到Zip并通过邮件问题发送它们C#

[英]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. 我创建了一种方法,该方法收集文件夹中的每个文件,然后将它们插入到zip文件中。 The zip file is then sent via mail to a customer. 然后将zip文件通过邮件发送给客户。 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. 我的问题是,我可以发送带有zip文件的邮件,而没有任何问题,但是,如果另一个用户生成了zip文件并发送,它将看起来像这样。

在此处输入图片说明

I had the same issue when trying to attach excel files to a mail message. 尝试将excel文件附加到邮件时遇到相同的问题。 But i found out that you have to set a ContentType of the files you attach. 但是我发现您必须设置要附加的文件的ContentType。 And after adding this line: ContentType ct = new ContentType("application/vnd.ms-excel"); 在添加以下行之后: ContentType ct = new ContentType("application/vnd.ms-excel"); for the excel files it worked. 对于它的Excel文件。

My problem is that i am trying to attach a zip file now, i am using this line: `ContentType ct = new ContentType("application/zip"); 我的问题是我现在尝试附加一个zip文件,我正在使用以下行:`ContentType ct = new ContentType(“ application / zip”); but this doesnt work. 但这不起作用。 I am using DotNetZipLib-DevKit-v1.9 to add files into a zip file. 我正在使用DotNetZipLib-DevKit-v1.9将文件添加到zip文件中。

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用户访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM