本文介绍了如何在asp.net的电子邮件正文中发送包含数据库数据的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一张表从表中返回25条记录。不,我想发送电子邮件,在邮件正文中我希望数据显示在表格中。
请帮忙怎么做。
谢谢和问候。
Hi All,
I have a table returning 25 records from a table. No I want to send an email and in message body I want data to be shown in table.
Please help how to do it.
Thanks and regards.
推荐答案
Hi,
try this method with your database settings assuming that the table is Employee
public void SendMail()
{
string body = "";
String ConnStr = "Data Source=ServerName;Initial Catalog=DBNamae;User ID=UserName;Password='password';";
String SQL = "SELECT ID, FirstName FROM Employee "
+ "WHERE ID IS NOT NULL";
SqlDataAdapter TitlesAdpt = new SqlDataAdapter(SQL, ConnStr);
DataSet Titles = new DataSet();
// No need to open or close the connection
// since the SqlDataAdapter will do this automatically.
TitlesAdpt.Fill(Titles);
body = "<table>";
foreach (DataRow Title in Titles.Tables[0].Rows)
{
body += "<tr>";
body += "<td>" + Title[0] + "</td>";
body += "<td>" + String.Format("{0:c}", Title[1])
+ "</td>";
body += "</tr>";
}
body += "</table>";
//now set up the mail settings
MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");
//can add more recipient
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
//add cc
message.CC.Add(new MailAddress("[email protected]"));
message.Subject = "This is my subject";
message.Body = body;
SmtpClient client = new SmtpClient();
client.Send(message);
}
这篇关于如何在asp.net的电子邮件正文中发送包含数据库数据的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!