Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got an application that scan certain directories for any file which exceeds certain size. After it detects it, it will send a warning email, stating that certain files have reached certain size limits. My problem is my current program will send one email per file. Meaning if there is 10 file that is over limit, then it will will send 10 email. How to make it compile all of the file and send a list of those file in a single email?Here is my code if needed:

C#
private void Form1_Load(object sender, EventArgs e)
        {
            count = 0;
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer1_Tick);
            timer.Start();
            

            List<string> s1 = System.IO.Directory.GetFiles(@"F:\gdimaging\data", "*.*", SearchOption.AllDirectories).ToList<string>();
            s1.AddRange(System.IO.Directory.GetFiles(@"F:\hios\DATA", "*.*", SearchOption.AllDirectories).ToList<string>());
            s1.AddRange(System.IO.Directory.GetFiles(@"F:\imgviewer\data", "*.*", SearchOption.AllDirectories).ToList<string>());
            s1.AddRange(System.IO.Directory.GetFiles(@"F:\newcnas\data", "*.*", SearchOption.AllDirectories).ToList<string>());
            s1.AddRange(System.IO.Directory.GetFiles(@"F:\newpod\data", "*.*", SearchOption.AllDirectories).ToList<string>());
            s1.AddRange(System.IO.Directory.GetFiles(@"F:\OMS\data", "*.*", SearchOption.AllDirectories).ToList<string>());
            s1.AddRange(System.IO.Directory.GetFiles(@"F:\WEBIMG", "*.*", SearchOption.AllDirectories).ToList<string>());
                     
            //s1 = Directory.GetFiles(@"F:\gdimaging\data", "*.*");
            dt.Columns.Add("File_Name");
            dt.Columns.Add("File_Type");
            dt.Columns.Add("File_Size");
            dt.Columns.Add("Create_Date");
            foreach(string s in s1)
            {
                try
                {
                    FileInfo info = new FileInfo(s);
                    FileSystemInfo sysInfo = new FileInfo(s);
                    dr = dt.NewRow();
                    //System.Collections.Generic.List<string> nameList;
                    dr["File_Name"] = sysInfo.Name;
                    dr["File_Type"] = sysInfo.Extension;
                    dr["File_Size"] = (info.Length / 1024).ToString();
                    dr["Create_Date"] = sysInfo.CreationTime.Date.ToString("dd/MM/yyyy");
                    dt.Rows.Add(dr);

                    
                    if ((info.Length / 1024) > 1500000)
                    {
                        //ArrayList exceed = new ArrayList();
                        //exceed.Add(sysInfo.Name).ToString();
                        //MessageBox.Show("" + sysInfo.Name + " HAS REACH IT SIZE LIMIT!!");
                        ///Basic Email message 
                        MailMessage mailMessage = new MailMessage();
                        // Email to send to
                        mailMessage.To.Add(new MailAddress("shahrul1509@yahoo.com"));
                        mailMessage.To.Add(new MailAddress("shahrul_kakashi90@hotmail.com"));
                        //MailAddress toEmail2 = new MailAddress("");
                        //mailMessage.To.Add(toEmail);
                        // set subject
                        mailMessage.Subject = "FILE SIZE WARNING MESSAGE";
                        // set body
                        mailMessage.Body = sysInfo.Name+ "HAS REACH ITS SIZE LIMIT.";
                        mailMessage.IsBodyHtml = true;
                        mailMessage.From = new MailAddress("**********", "Shahrul Nizam");
                        // Identify the credentials to login to the gmail account  
                        string sendEmailsFrom = "*********";
                        string sendEmailsFromPassword = "**********";
                        NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
                        SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
                        mailClient.EnableSsl = true;
                        mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                        mailClient.UseDefaultCredentials = false;
                        mailClient.Timeout = 20000;
                        mailClient.Credentials = cred;
                        mailClient.Send(mailMessage);
                        MessageBox.Show("Email Notification Sent!");
                    }

                    if (dt.Rows.Count > 0)
                    {
                        dataGridView1.DataSource = dt;
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    MessageBox.Show("Error : " + ex.Message);
                    continue;
                }
                
            }
        }
Posted
Updated 15-Apr-12 18:02pm
v2

Create a string variable for your email message body.

Inside the foreach loop, just append file names (or whatever data you want to) to the string.

After the foreach loop, check your count to see if you added anything to the string and send the email if necessary using the string as the body.
 
Share this answer
 
Comments
nizam15 16-Apr-12 0:01am    
Just to ask an opinion, is it advisable for me to move the email sending part of my code outside the loop, then within the file size checking using if, I append all of the file which exceeds the size into a List, then I just use the List to send the email??(sorry I'm still new to programming)
nizam15 16-Apr-12 2:34am    
I've edited my code. I tried to use ArrayList and I use a MessageBox instead of sending email just to test whether it works or not. Now when I use ArrayList, when I run the program, it pop up a message :
System.Collection.Arraylist is overlimit.
It suppose to pop up :
"fileName" is overlimit."
but it didn't. Can point out to me what's wrong?
In general, if you modify your code, you should re-post it so readers don't have to guess what you mean.

From your description, you probably have something like:
C#
myArrayList[i].ToString() + " is over limit";

and what you need is:
C#
myArrayList[i].fileEntry.name + " is over limit";
 
Share this answer
 
Comments
nizam15 16-Apr-12 21:01pm    
Sorry about that. I've edited my code but don't know why it didn't save the change. By the way thanks for the advice.:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900