-1

I am able to save all of my files into a zip file and download them correctly. Now I am needing to use that zip file to attach to an email. I am getting errors saying it can't bee opened and if i open it in notepad they are blank. They are pdfs.

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:

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?

GSerg
  • 76,472
  • 17
  • 159
  • 346

1 Answers1

0

Please use correct format in the attachment. you could try mail.Attachments.Add(new Attachment(output, "Client Statments.zip"));