AnswerBun.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!

Related Questions

Flight destination check programm – C++

3  Asked on January 12, 2021 by mrfreeman16

   

How to execute (./myscript) inside awk or bash script?

3  Asked on January 12, 2021 by redouane-nouh

   

Disable nuxt link based on boolean

3  Asked on January 12, 2021 by samantha

     

Android Preventing Double Click On A Layout with handher

1  Asked on January 12, 2021 by user13380366

     

How to center align the label text inside the option tag?

1  Asked on January 11, 2021 by mr-spock

         

How can I read the CSS for mobile images

0  Asked on January 11, 2021 by power-rico

   

Deleting from array using unset creates values

2  Asked on January 11, 2021 by cameron

 

passport.socket.io cant fix no session found

2  Asked on January 11, 2021 by edi_hadzic

         

Output should look like a quotation

4  Asked on January 11, 2021 by maurya

   

Sorting DateTime column in ASP.NET MVC

2  Asked on January 11, 2021 by steven85791

     

Rounding does not match all expected results

1  Asked on January 11, 2021 by aa-44

   

Django tabular input form (user can key in data into table)

1  Asked on January 11, 2021 by eka-buyung-lienadi

 

Ask a Question

Get help from others!

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