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:
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>());
dt.Columns.Add("File_Name");
dt.Columns.Add("File_Type");
dt.Columns.Add("File_Size");
dt.Columns.Add("Create_Date");
ArrayList fileList = new ArrayList();
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)
{
fileList.Add(sysInfo.Name);
}
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show("Error : " + ex.Message);
continue;
}
}
MessageBox.Show(fileList + "overlimit!!");
}
Encapsulate the Mail options in a class
with a public method/attribute that returns a boolean if one of the files meets the criteria and only perform the mail operations if the criteria are met:
something like this — not checked as it was done on a mac in a text editor