如何创建新的gridview并在代码后面填充,并作为电子邮件正文发送

本文关键字:填充 正文 电子邮件 创建 何创建 gridview 代码 | 更新日期: 2023-09-27 18:11:32

如何创建新的gridview/datalist并在代码后面填充并作为email body发送?我已经有一个DataList填充了项目,但我不想发送它的所有列。所以,我想生成一个gridview与我想要的列发送电子邮件。下面是我为

所做的
            GridView gv = new GridView();
            DataTable tbl = new DataTable();
            tbl.Columns.Add("Items", typeof(string));
            tbl.Columns.Add("Price", typeof(string));
            tbl.Columns.Add("Total", typeof(string));
            List<HiredList> hl = new List<HiredList>();
            hl = (List<HiredList>)Session["Hired"];
            foreach (var i in hl)
            {
                DataRow itms = tbl.NewRow();
                itms["Items"] = i.pName;
                itms["Price"] = i.price;
                itms["Total"] = i.pTotal;
            }
            //use AutoGenerateColumnsto let grid generate the column itself'
            gv.AutoGenerateColumns = true;
            gv.DataSource = tbl;
            gv.DataBind();

然后发送这个gridview到

private string GetGridviewData(GridView gvList)
    {
        StringBuilder strBuilder = new StringBuilder();
        StringWriter strWriter = new StringWriter(strBuilder);
        HtmlTextWriter htw = new HtmlTextWriter(strWriter);
        gvList.RenderControl(htw);
        return strBuilder.ToString();
    }

但是作为回报,我只收到了我收到的邮件。我不知道我哪里出错了

如何创建新的gridview并在代码后面填充,并作为电子邮件正文发送

以邮件的形式发送gridview中的数据:

 public void SendHTMLMail()
{
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("administrator@aspdotnet-suresh.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress("suresh@gmail.com"));
// Subject of e-mail
Msg.Subject = "Send Gridivew in EMail";
Msg.Body += "Please check below data <br/><br/>";
Msg.Body += GetGridviewData(gvUserInfo);
Msg.IsBodyHtml = true;
string sSmtpServer = "";
sSmtpServer = "10.2.160.101";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.EnableSsl = true;
a.Send(Msg);
}

//这个方法用于渲染gridview控件

public string GetGridviewData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}

参考链接:

http://www.aspdotnet-suresh.com/2012/09/how-to-send-gridview-in-email-body-in.html

For Plain HTML Page As Mail:

http://www.aspdotnet-suresh.com/2012/09/how-to-send-html-page-as-email-body-in.html

datalist

:

http://www.dreamincode.net/forums/topic/269410-send-datalist-value-within-contact-page/