C# 如何创建新的gridview并填充代码隐藏和作为电子邮件正文发送?

C# 如何创建新的gridview并填充代码隐藏和作为电子邮件正文发送?,c#,asp.net,C#,Asp.net,如何创建新的gridview/datalist,在代码隐藏处填充并作为电子邮件正文发送? 我已经用item填充了一个DataList,但我不想发送其中的所有列。因此,我想生成一个gridview,其中包含我希望发送电子邮件的列。 下面是我这样做的原因 GridView gv = new GridView(); DataTable tbl = new DataTable(); tbl.Columns.Add("Items",

如何创建新的
gridview/datalist
,在代码隐藏处填充并作为
电子邮件正文发送?
我已经用item填充了一个
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中以邮件形式发送数据:

 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);
}
参考链接:

 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);
}

对于邮件形式的普通HTML页面:

 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);
}

对于数据列表:

 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);
}


希望对您有所帮助。

到目前为止您试过什么吗?请阅读,我可以发送数据列表吗?