更新时间:2022-12-03 10:59:42
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("sender@foo.bar.com");
//can add more recipient
message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
//add cc
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = body;
SmtpClient client = new SmtpClient();
client.Send(message);
}
MailMessage objMailMessage = new MailMessage();
objMailMessage.IsBodyHtml = false;
将IsBodyHtml = false的标志更改为IsBodyHtml = true,因为您必须以表格格式发送邮件。
MailMessage objMailMessage = new MailMessage();
objMailMessage.IsBodyHtml = false;
change flag of IsBodyHtml=false to IsBodyHtml=true because you have to send mail in tabular format.