Search code examples
c#asp.net-mvcsmtpclientdotnetzip

Email attachment is being sent as a file instead of a zip file


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]


Solution

  • 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";