0

我有一个应用程序可以扫描某些目录以查找任何超过特定大小的文件。检测到它后,它会发送一封警告电子邮件,说明某些文件已达到一定的大小限制。我的问题是我当前的程序将为每个文件发送一封电子邮件。这意味着如果有 10 个文件超出限制,那么它将发送 10 封电子邮件。如何使它编译所有文件并在一封电子邮件中发送这些文件的列表?如果需要,这是我的代码:

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!!");

}

4

2 回答 2

1

使用公共方法/属性将 Mail 选项封装在一个类中,如果其中一个文件满足条件,则返回布尔值,并且仅在满足条件时才执行邮件操作:

像这样的东西 - 没有检查,因为它是在文本编辑器中的 mac 上完成的

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");
    // new mail list class
    class mailList {
    public bool isEmpty = true;
    MailMessage mailMessage;
    // do all the onceonly stuff in the constructor
        public mailList(){
            mailMessage = new MailMessage();
            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";
            // Identify the credentials to login to the gmail account  
            string sendEmailsFrom = "shahrul1509@gmail.com";
                    // password below is written in * to encrypt it

            string sendEmailsFromPassword = "***4556**";
            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;
            mailMessage.IsBodyHtml = true;
            mailMessage.From = new MailAddress("shahrul1509@gmail.com", "Shahrul Nizam");

        }
        public addFile(String fileName){
            mailMessage.Body = mailMessage.Body + sysInfo.Name+ "HAS REACH ITS SIZE LIMIT.";
            isEmpty = false;
        }
        public send(){
            mailClient.Send(mailMessage);
            MessageBox.Show("Email Notification Sent!");
        }
    }

    }
    try
        {
        mailList ml = new mailList();
        foreach(string s in s1)
            {
                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)
                {
                    ml.addFile(sysInfo.Name);

                    if (dt.Rows.Count > 0)
                    {
                    dataGridView1.DataSource = dt;
                    }

                }
                if(ml.isEmpty==false){
                    ml.Send();
                }
            }
    catch (UnauthorizedAccessException ex)
    {
        MessageBox.Show("Error : " + ex.Message);
        continue;
    }

}
于 2012-04-16T04:41:14.137 回答
1

在 foreach 循环中检查文件大小后,然后将这些超出特定限制的文件添加到另一个数组中,并将数组元素加入到位于 for 循环之外的消息正文中。

于 2012-04-16T03:52:36.647 回答