通过电子邮件并排发送两个列表?

时间:2012-04-20 02:28:32

标签: c# winforms

我这里有点问题。我有一个应用程序,它通过电子邮件发送文件名列表,通知用户这些文件超过了一定的大小限制。但现在我希望它不仅可以发送文件名,还可以发送文件大小。我设法将文件大小附加到电子邮件中。但我的问题是它的显示方式。我希望它显示如下:

THE FILES :

filename1 = filesize1
filename2 = filesize2
filename3 = filesize3
..
..
..

HAS REACHED ITS LIMITS!

但我目前在电子邮件中的显示格式如下:

THE FILES :

filename1
filename2
filename3
filesize1
filesize2
filesize3

HAS REACHED ITS LIMITS!

现在我不知道如何更改第一个显示格式。任何帮助将不胜感激。 这是我的代码片段:

private void Form1_Load(object sender, EventArgs e)
    {   
        count = 0;
        timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += new EventHandler(timer1_Tick);
        timer.Start();
        System.Collections.Generic.List<string> files = new List<string>();
        System.Collections.Generic.List<string> files1 = new List<string>();
        //List<string> s1 = System.IO.Directory.GetFiles(@"C:\Documents and Settings\Administrator\Desktop\test", "*.*", SearchOption.AllDirectories).ToList<string>();
        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");

        foreach (string s in s1) 
        {   
            try
            {   
                FileInfo info = new FileInfo(s);
                FileSystemInfo sysInfo = new FileInfo(s);
                dr = dt.NewRow();
                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)
                {
                    MyFiles = new Dictionary<string, string>();
                    //files.Add(sysInfo.Name.ToString());
                    //files1.Add(info.Length.ToString());
                    //arr = string.Join("<br/>", files.ToArray());
                    //arr1 = string.Join("<br/>", files1.ToArray());
                    MyFiles.Add(sysInfo.Name.ToString(), info.Length.ToString());
                }

                if (dt.Rows.Count > 0)
                {
                    dataGridView1.DataSource = dt;
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show("Error : " + ex.Message);
                continue;
            }
        }

        if (arr != null)
        {
            ///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 FILES : <br/><br/>" + arr + arr1 + "<br/><br/> HAS REACH ITS SIZE LIMIT!!";
            mailMessage.Body = "THE FILES : <br/><br/>";
            foreach (string key in MyFiles.Keys)
            {
                mailMessage.Body += key + " = " + MyFiles[key] + "<br/>";
                mailMessage.Body += "<br/> HAS REACHED 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.UseDefaultCredentials = true;
            mailClient.Credentials = cred;
            mailClient.Send(mailMessage);
            //MessageBox.Show("Email Notification Sent!");
            //MessageBox.Show(fileList.ToString() + "overlimit!!");
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
            count++;
            if (count == 600)
            {
                count = 0;
                timer.Stop();
                Application.Restart();
            }
    }

1 个答案:

答案 0 :(得分:1)

改变这个:

/*step 1*/if ((info.Length / 1024) > 1500000)
                {//my size checking and storing it in an array
                    files.Add(sysInfo.Name.ToString());
                    files1.Add(info.Length.ToString());
                    arr = string.Join("<br/>", files.ToArray());
                    arr1 = string.Join("<br/>", files1.ToArray());
                }
/*step 2*/mailMessage.Body = "THE FILES : <br/><br/>" + arr + arr1 + "<br/><br/> HAS REACH ITS SIZE LIMIT!!";

/*step 1*/if ((info.Length / 1024) > 1500000)
                {//my size checking and storing it in an array
                    files.Add(sysInfo.Name.ToString());
                    files1.Add(info.Length.ToString());
                }
/*step2*/mailMessage.Body = "THE FILES : <br/><br/>";
for(int i=0; i<files.Count; i++)
  mailMessage.Body += files[i] + " = " + files1[i] + "<br/>;
mailMessage.Body += "<br/> HAS REACHED ITS SIZE LIMIT!!";

列表最后一个实例的额外<br/>无关紧要,因为最后一行以2个中断开始(我删除了一个)。

我做的假设:

  • 列表大小相同
  • 一个中的每个索引对应于另一个中的项目。

使用DICTIONARY(类型)可以更好地完成此操作:

/*step 1*/if ((info.Length / 1024) > 1500000)
                {//my size checking and storing it in an array
                    /* I suppose there is a Dictionary<string, string> MyFiles;
                    MyFiles = new Dictionary<string,string>(); instantiated like so*/
                    MyFiles.add(sysInfo.Name.ToString(), info.Length.ToString());
                }
                //attaching the array to the email body
                    /*step 2*/mailMessage.Body = "THE FILES : <br/><br/>";
                    foreach(string key in MyFiles.Keys)
                      mailMessage.Body += key + " = " + MyFiles[key] + "<br/>";
                    mailMessage.Body += "<br/> HAVE REACHED THEIR SIZE LIMIT!!";

请注意,通过更好的我做 NOT 意味着更高的内存效率。通过更好,我的意思是更不容易出错。从技术上讲,你有一个键值配对。那是一本字典。