TransWikia.com

Break apart email attachment if zip is too big to email

Stack Overflow Asked by ProgrammingIsLife on January 1, 2022

So I can send up to 25MB in an email attachment. I am looking for away to check how big the file size is and if its too big break it apart and send in two emails. (Or if there is a better way to do it)?

Here is where I am sending the email with the attachment:

public static void SendEmail(List<string> recipients, MemoryStream output, string from, string subject, string htmlMessage, bool isHtml = true)
    {
        var host = ConfigurationManager.AppSettings["emailHost"];
        var emailFrom = ConfigurationManager.AppSettings["FromEmail"];
        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, "ClientStatements.zip");
            mail.Attachments.Add(myZip);
            SmtpServer.Send(mail);
        }
        catch (Exception ex)
        {
           FMBUtilities.Logger.LogErrorToSql2012PrdAndEmailTeam("DBQueries", "SendEmail", ex);
        }
    }

Here is where i am creating the zip file to send:

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}.nn * 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 found some things about setting the file size in the web.config file and things of that sort but just dont know the best way to go about this.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP