【发布时间】:2013-03-12 03:52:31
【问题描述】:
我创建了一种方法,可以收集文件夹中的每个文件,然后将它们插入到 zip 文件中。然后通过邮件将 zip 文件发送给客户。我的问题是我可以毫无问题地发送带有 zip 文件的邮件消息,但是如果另一个用户生成 zip 文件并发送它,它将看起来像这样。
我在尝试将 excel 文件附加到邮件消息时遇到了同样的问题。但我发现您必须设置附加文件的 ContentType。在添加这一行之后:ContentType ct = new ContentType("application/vnd.ms-excel"); 为它工作的 excel 文件。
我的问题是我现在正在尝试附加一个 zip 文件,我正在使用这一行:`ContentType ct = new ContentType("application/zip");但这不起作用。我正在使用 DotNetZipLib-DevKit-v1.9 将文件添加到 zip 文件中。
这是我的代码:
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;
}
}
}
}
【问题讨论】:
-
在 using 中调用
Dispose是不必要的。 using 块会自动调用它。 -
尝试在
attachment上设置MIME 标头Content-Disposition: attachment; filename=filename.zip;。这可能与 Outlook 处理附件的方式有关。 -
我应该如何在 Windows 窗体应用程序中做到这一点?
标签: c# .net zip content-type