I got an application where by it will monitor all the files in multiple directories. Whenever there is files that exceeds a certain size limit, it will send warning email notification. Currently, my program will send an email for each oversize file. Meaning if there is 20 files that is oversize, then it will send 20 emails. Can someone teach how to store all the file name within a single variable and send a single email which tells the list of files name that is oversized. Here is my source :
private void Form1_Load(object sender, EventArgs e)
{
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>());
dt.Columns.Add("File_Name");
dt.Columns.Add("File_Type");
dt.Columns.Add("File_Size");
dt.Columns.Add("Create_Date");
List<string> files = new List<string>();
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)
{
files.Add(sysInfo.Name.ToString()); //Here is the part where I tried to store the list of files name.
}
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show("Error : " + ex.Message);
continue;
}
}
//fileList.Add(sysInfo.Name).ToString();
//fileList.Add(sysInfo.Name);
///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"));
//set subject
mailMessage.Subject = "FILE SIZE WARNING MESSAGE";
//set body
mailMessage.Body = "THE FILE HAS REACH ITS SIZE LIMIT!!";
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress("shahrul1509@gmail.com", "Shahrul Nizam");
//Identify the credentials to login to the gmail account
string sendEmailsFrom = "shahrul1509@gmail.com";
string sendEmailsFromPassword = "0137089732";
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!");
///MessageBox.Show(fileList.ToString() + "overlimit!!");
}`