[英]Zip File not able to open after attaching to email using dotnetzip library
I am able to save all of my files into a zip file and download them correctly.我能够将所有文件保存到 zip 文件中并正确下载。 Now I am needing to use that zip file to attach to an email.现在我需要使用 zip 文件附加到 email。 I am getting errors saying it can't bee opened and if i open it in notepad they are blank.我收到错误说它无法打开,如果我在记事本中打开它,它们是空白的。 They are pdfs.它们是pdf。
Here's how I am downloading them:这是我下载它们的方式:
else if (radio[0] == "Email Statements")
{
// Make array of emails into List for sending in email
if (emails.ToString() != "")
{
var allEmails = emails[0].Split(',');
foreach (var email in allEmails)
{
if (emailValid.IsMatch(email))
{
everyEmail.Add(email);
}
else
{
return Json(new { success = false, message = $"* Not valid email address: {email}.\n\n * Please double check and try again." });
}
List<string> distinctFiles = allPaths
.GroupBy(x => x.Split(new char[] { '\\' }).Last())
.Select(x => x.First())
.ToList();
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(distinctFiles, @"\");
MemoryStream output = new MemoryStream();
output.Position = 0;
zip.Save(output);
DBQueries.SendEmail(everyEmail, output, fromAddress, "Client Statement Reports", "Here are your requested Client Statements", true);
So the DBQueries.SendEmail
function will bring me to this:所以DBQueries.SendEmail
function 将把我带到这个:
public static void SendEmail(List<string> recipients, MemoryStream output, string from, string subject, string htmlMessage, bool isHtml = true)
{
var host = ConfigurationManager.AppSettings["emailHost"];
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
foreach (var r in recipients)
{
mail.To.Add(r);
}
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = htmlMessage;
//string result = System.Text.Encoding.UTF8.GetString(output.ToArray());
SmtpClient SmtpServer = new SmtpClient(host);
SmtpServer.Port = 25;
mail.Attachments.Add(new Attachment(output, "Client Statments"));
SmtpServer.Send(mail);
}
catch (Exception ex)
{
FMBUtilities.Logger.LogErrorToSql2012Prd("DBQueries", "SendEmail", ex);
}
}
I am not able to read the attachment after this.在此之后我无法阅读附件。 What do I need to do to make this readable?我需要做什么才能使其可读?
Please use correct format in the attachment.请在附件中使用正确的格式。 you could try mail.Attachments.Add(new Attachment(output, "Client Statments.zip"));你可以试试 mail.Attachments.Add(new Attachment(output, "Client Statments.zip"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.