0

So I am trying to attach a zip file of pdfs to an email and it comes in as a file not as a zip. I can open it in notepad but its just a bunch of random characters.

Here is my method to send the email:

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;
        

        Attachment myZip = new Attachment(output, "Client Statement");
        mail.Attachments.Add(myZip);
        SmtpServer.Send(mail);
    }
    catch (Exception ex)
    {
       FMBUtilities.Logger.LogErrorToSql2012PrdAndEmailTeam("DBQueries", "SendEmail", ex);
    }
}

I am calling this from my controller here:

// 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." });
        }
        MemoryStream output = new MemoryStream();

        List<string> distinctFiles = allPaths
            .GroupBy(x => x.Split(new char[] { '\\' }).Last())
            .Select(x => x.First())
            .ToList();
        using (ZipFile zip = new ZipFile())
        {
              
            zip.AddFiles(distinctFiles, @"\");
            
            zip.Save(output);
            output.Position = 0;
            
            DBQueries.SendEmail(everyEmail, output, fromAddress, "Client Statement Reports", "Here are your requested Client Statements", true);

        }

I have another controller method that I use to download the zip file so I know that it is downloading correctly and everything is fine. I just cant seem to figure out how to attach this zip to an email.

EDIT:

Here is an image of the file thats coming in from the attachment. [![enter image description here][1]][1]

  • a zip is only a file, too. have you tried renaming your result to .zip and opening it? probably just the name didn't get transmitted. – Franz Gleichmann Jul 23 '20 at 18:46
  • What does "a file not a zip file" mean? How is a "zip file" not a subset of "file"? Most crucially, please show a screenshot of the file you end up seeing as attached, open in a hex editor. I would like to have a look at the bytes inside it – Caius Jard Jul 23 '20 at 18:47
  • The file starts with "PK" - it is a ZIP file as Franz surmised – Caius Jard Jul 23 '20 at 18:54
  • 1
    @FranzGleichmann after editing my first response i tried renaming it to .zip. I opened it up and all of my files were there!!! What the heck! Amazing! Now I just need to make sure that it gets sent as a zip. Easy way to do this? If you make an answer post I would be happy to accept it and give you the points. Thank you so much! – ProgrammingIsLife Jul 23 '20 at 18:56
  • @CaiusJard yes i renamed the file and everything is perfect!! Now i just need to figure out how to make sure it sends as a zip. – ProgrammingIsLife Jul 23 '20 at 18:57

2 Answers2

2

The file starts with "PK" - it is a zip, but because you made it from a memory stream and don't appear to have specified a filename it will just come through like that with a default name

Have a look for a ContentDisposition property of the attachment and a FileName property on that, eg

myZip.ContentDisposition.FileName = "myFile.zip";
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

It looks like it actually is a zip file. But without the file extension .zip. I guess there is a way to name the file manually to provide a name with the .zip extension (e.g. test.zip)