如何将文件名列表存储到一个列表中并通过电子邮件发送?

时间:2012-04-16 03:38:37

标签: c# winforms

我有一个应用程序扫描某些目录中任何超过一定大小的文件。检测到后,它会发送一条警告电子邮件,说明某些文件已达到一定的大小限制。我的问题是我当前的程序将每个文件发送一封电子邮件。意思是如果有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!!");

}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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;
    }

}