【问题标题】:How to add a grid to an email如何在电子邮件中添加网格
【发布时间】:2016-09-26 09:06:50
【问题描述】:

代码如下,请问如何将网格添加到电子邮件中。

private void button1_Click(object sender, EventArgs e)
{
}

private void Form1_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();

    conn.ConnectionString = @"Data Source=10.1.1.1;Initial Catalog=xxx;Integrated Security=True";
    SqlCommand command = new SqlCommand();
    command.Connection= conn;
    command.CommandText ="Select top 10 * from tablea";

    DataTable data = new DataTable();
    SqlDataAdapter adapter = new SqlDataAdapter(); 
    adapter.SelectCommand = command;
    adapter.Fill(data);
    gvSendEmail.DataSource = data;   

    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("10.1.1.11");
        mail.From = new MailAddress("xxxx@xxxxx", "xxxx");
        mail.To.Add("xxxxx@xxxxx.xxx");
        mail.Subject = "Test Mail";
        mail.Body = "This is test email";
        mail.Body += "Please check below data ";
        SmtpServer.Port = 25;
        SmtpServer.Credentials = new System.Net.NetworkCredential("xxxx", "xxxx");
        SmtpServer.EnableSsl = false;
        SmtpServer.Send(mail);
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

【问题讨论】:

    标签: c# datagridview mail-sender


    【解决方案1】:

    尝试以下方法:

    private void btnSend_Click_1(object sender, EventArgs e)
     {
          string mailBody = "<table width ='100%' style ='border:Solid 1px Black;'>";
    
          foreach (DataGridViewRow row in dataGridView1.Rows) //Loop through DataGridView to get rows
          {
                mailBody += "<tr>";
                foreach (DataGridViewCell cell in row.Cells) //Loop through DataGridView to get cell value
                {
                   mailBody += "<td stlye='color:blue;'>" +cell.Value + "</td>";
                }
                mailBody += "</tr>";
          }
          mailBody += "</table>";
    
          //Finally rest of the original code
          mail.IsBodyHtml = true; 
          mail.Body = mailBody;
          client.Send(mail); 
     }      
    

    【讨论】:

    • 您必须将 MailMessage 对象的 IsBodyHtml 属性设置为 True。 mail.IsBodyHtml = true;
    • 已更新。感谢您的通知。
    • 只是一件小事,表格没有显示网格标题列。谢谢。
    • 只需像这样将它与 mailBody 连接起来:mailBody += "Table Header";我想,这就是你想要的,
    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    相关资源
    最近更新 更多