C#每行都会向行收件人发送电子邮件

时间:2019-07-10 19:28:31

标签: c# email datagridview cell rows

如何从datagridview行发送每个单元格值? Example

示例: vasia@mail.ru收件人得到: Vasia欠100欧元。 liuda @ mailru Liuda欠23欧元。

我的代码发送给多用户:

 private void btnSendMail_Click(object sender, EventArgs e)
    {


        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),new DataColumn("Name", typeof(string)),
                    new DataColumn("Debt", typeof(string)),
                    new DataColumn("Email",typeof(string)) });

        dt.Rows.Add("Vasia", "130", "vasia@mail.com");
        dt.Rows.Add("Liuda", "23", "liuda@test.com");
        dt.Rows.Add("Grisa", "2560", "grisa@test.com");
        GridView1.DataSource = dt;


        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string emailId = dt.Rows[i]["email"].ToString();
            SendEmailUsingGmail(emailId);
        }

        //write code to send mail

    }
    private void SendEmailUsingGmail(string toEmailAddress)
    {
        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("xxx.xxx.xx");

            mail.From = new MailAddress("xxxxxa@xxxx.ru");
            mail.To.Add(toEmailAddress);
            mail.Subject = "tgggggg ";
            mail.Body = "Aš,  papildžiau planą šiais produktais:  PRAŠAU PATIKRINKITE PLANĄ !!!";

            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("xxxxx@xxx.ru", "xxxxx");
            //SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            MessageBox.Show("Informavimo laiškas išsiųstas Tiekimo skyriui bei Gamybai");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

如何将正文中的每个单元格值从datagridview行发送到每封电子邮件中?

2 个答案:

答案 0 :(得分:0)

您需要构建电子邮件正文字符串,然后遍历电子邮件。我对此做了些微修改:

        private void btnSendMail_Click(object sender, EventArgs e)
        {


            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),new DataColumn("Name", typeof(string)),
                    new DataColumn("Debt", typeof(string)),
                    new DataColumn("Email",typeof(string)) });

            dt.Rows.Add("Vasia", "130", "vasia@mail.com");
            dt.Rows.Add("Liuda", "23", "liuda@test.com");
            dt.Rows.Add("Grisa", "2560", "grisa@test.com");
            GridView1.DataSource = dt;

            string bodyText = BuildStringFromDT(dt);

            foreach (DataRow row in dt.Rows)
            {
                string emailId = row["Email"].ToString();
                SendEmailUsingGmail(emailId, bodyText);
            }

            //write code to send mail

        }
        private string BuildStringFromDT(DataTable dt)
        {
            List<string> final = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                List<string> temp = new List<string>();
                foreach (DataColumn col in dt.Columns)
                {
                    temp.Add(row[col.ColumnName].ToString());
                }
                final.Add(string.Join("\t", temp));
            }
            return string.Join("\r\n", final);
        }
        private void SendEmailUsingGmail(string toEmailAddress, string bodyText)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("xxx.xxx.xx");

                mail.From = new MailAddress("xxxxxa@xxxx.ru");
                mail.To.Add(toEmailAddress);
                mail.Subject = "tgggggg ";
                mail.Body = bodyText;

                SmtpServer.Port = 25;
                SmtpServer.Credentials = new System.Net.NetworkCredential("xxxxx@xxx.ru", "xxxxx");
                //SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("Informavimo laiškas išsiųstas Tiekimo skyriui bei Gamybai");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

答案 1 :(得分:0)

我建议不要在代码中使用纯文本Body属性,而是建议编写一个基于HTML的消息正文,在其中可以正确放置表格。

如果您使用的是System.Net.Mail.MailMessage,则可以使用:

mail.IsBodyHtml = true;

请注意,System.Web.Mail.MailMessage已过时,但如果使用它:

mail.BodyFormat