将datagridview中的选定行发送到电子邮件

时间:2014-08-28 11:44:56

标签: c# winforms email datagridview

我目前正在解决我在C#Winform应用程序中遇到的一段时间的问题。我有一个数据网格,其中包含通过查询填充的信息。一切都运行良好,winform的用户可以" tick"将通过电子邮件发送到其他地方的相关行。我遇到的问题是,目前,所有行都将包含在电子邮件中,无论是否勾选。我想只将勾选的行发送到电子邮件,其余的被忽略。我觉得我很亲密,我的代码如下:

private void btnEmailReg_Click(object sender, EventArgs e)
    {
        Cursor.Current = Cursors.WaitCursor;
        try
        {
            SmtpClient client = new SmtpClient("smtpclient");
            MailMessage message = new MailMessage();
            message.From = new MailAddress("address");
            string mailBox = cmbExamTeamReg.Text.Trim();
            message.To.Add(mailBox);
            string mailFrom = txtEmailFromReg.Text.Trim();
            message.CC.Add(mailFrom);
            string mailCC = txtMailCCReg.Text.Trim();
            message.Bcc.Add(mailCC);
            File.Delete("Registration_Result.xls");
            StreamWriter sw = new StreamWriter("Registration Result.xls", false, Encoding.UTF8);
            sw.Write(htmlMessageBodyReg().ToString());
            sw.Close();
            Attachment myAttach = new Attachment("Registration Result.xls");
            message.Attachments.Add(myAttach);
            message.IsBodyHtml = true;
            message.Body = "Registration request, please see following details: " + "\n" + "<br>" +
                "Exam Team member: " + cmbExamTeamReg.Text +
                "\n" + "<br>" + "Course Tutor: " + txtCourseTutorReg.Text +
                "\n" + "<br>" + "Award & Complex Number: " + txtAwardCompReg.Text +
                "\n" + "<br>" + "Comments: " + rtextCommentsReg.Text;
            message.Subject = "Registration Request";
            client.Credentials = new System.Net.NetworkCredential("credentials);
            client.Port = System.Convert.ToInt32(25);
            for (int i = 0; i < this.dgCourseStudentsReg.Rows.Count; i++)
            {
                if (dgCourseStudentsReg.Rows[i].Cells[0].Value.ToString() == "True")
                {
                    for (int j = 1; j < this.dgCourseStudentsReg.Columns.Count; j++)
                    {
                        message.Body = message.Body + "\n" + "<br>" + this.dgCourseStudentsReg.Rows[i].Cells[j].Value;
                    }

                    dgCourseStudentsReg.Rows[i].Selected = true;
                    dgCourseStudentsReg.Rows[i].Visible = true;
                }
                else 
                {
                    dgCourseStudentsReg.CurrentCell = null;
                    dgCourseStudentsReg.Rows[i].Selected = false;
                    dgCourseStudentsReg.Rows[i].Visible = false;
                }
            }          
            client.Send(message);
            new Endpage().Show();
            this.Close();
        }
        catch
        {
            MessageBox.Show("Please check all required fields are complete");
        }
    }

我也在专家交流的帮助下完成了这项工作,可以找到here主题以获取更多详细信息;

目前的情况是,如果我更改某些内容(所有行都通过)或出现错误(到目前为止主题末尾最详细的演练),或者它确实有效,代码将无效,它只会隐藏选定的行而不是电子邮件。

1 个答案:

答案 0 :(得分:0)

您需要加载数据,然后在datagridview中选择要发送电子邮件的行,然后单击电子邮件按钮,我已尽可能简单地为您编写代码,如下所示:

private void Form1_Load(object sender, EventArgs e)
{
 DataTable dt = new DataTable();

 dt.Columns.Add("Id");
 dt.Columns.Add("Student Name");
 dt.Columns.Add("Address");
 dt.Columns.Add("Course");


 dt.Rows.Add("1", "Heisenberg", "123 Somwhere Lane, Alburque, NM-12345", "Chemistry101");

 dt.Rows.Add("2", "Jesse Pinkman", "N/A", "Meth101");

 dt.Rows.Add("3", "Hank" , "N/A 2", "DEA101");

 dt.Rows.Add("4", "Guss Fring", "Los Pollos Hermanos", "ChickenMaking102");

 dt.Rows.Add("5", "John Smith", "123 Nowhere Lane, Nowhereland, NW-12345", "English101");


 dataGridView1.DataSource = dt;
}





    private void btnEmail_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            string value1 = row.Cells[0].Value.ToString();
            string value2 = row.Cells[1].Value.ToString();
            string value3 = row.Cells[2].Value.ToString();
            string value4 = row.Cells[3].Value.ToString();

            this.Cursor = Cursors.WaitCursor;


            SmtpClient client = new SmtpClient("smtpclient");


            // Specify the e-mail sender. 
            var from = new MailAddress("from@test.com");

            // Set destinations for the e-mail message.
            var sendTo = "to@test.com";

            // Specify the message content.
            var message = new MailMessage();

            message.From = from;

            message.To.Add(sendTo);

            message.Body = "<b>EMAIL MESSAGE</b><br/>" + "Put your email here!<br/> " + value1 + " " + value2 + " " + value3 + " " + value4;

            message.Subject = "Your email subject goes here!";

            message.IsBodyHtml = true;

            client.Send(message);

            // Clean up.
            message.Dispose();

            MessageBox.Show("Email Notification Sent!", "Message Sent", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

            this.Cursor = Cursors.Default;

        }

    }