C# 如何使用变量从数据表中列出多条数据
我编写了以下程序,向此表中的演示用户发送HTML电子邮件: 我只想发送一封电子邮件,列出每个客户的所有信息。目前,如果一个客户有10本过期的书,那么该客户将收到10封基于表结构的电子邮件,因为每个图书贷款都是一个单独的记录 例如: 亲爱的吉姆·凯里: 以下书籍已到期:C# 如何使用变量从数据表中列出多条数据,c#,datatable,datarow,C#,Datatable,Datarow,我编写了以下程序,向此表中的演示用户发送HTML电子邮件: 我只想发送一封电子邮件,列出每个客户的所有信息。目前,如果一个客户有10本过期的书,那么该客户将收到10封基于表结构的电子邮件,因为每个图书贷款都是一个单独的记录 例如: 亲爱的吉姆·凯里: 以下书籍已到期: 如何修理汽车 詹姆斯·邦德回归 有没有一种方法可以让我只为每个客户发送一封电子邮件,但在电子邮件中列出每个项目。例如,列出所有到期的书 Fox示例:使用上表发送电子邮件时,Jim Carey将收到2封电子邮件,而不是1封。我只想
class Program
{
static DataSet dtProfile = Database.AcquireData();
static DataTable table = dtProfile.Tables[0];
static string CustFName;
static string CustLName;
static string CheckoutDate;
static string DueDate;
static string BookName;
public static void SendEmail()
{
foreach (DataRow row in table.Rows)
{
CustFName = row["CustFName"].ToString();
CustLName = row["CustLName"].ToString();
CheckoutDate = row["CheckoutDate"].ToString();
DueDate = row["DueDate"].ToString();
BookName = row["BookName"].ToString();
string body = PopulateBody(CustFName, CustLName, CheckoutDate, DueDate, BookName);<br />
SendHtmlFormattedEmail("Email", "Subject", body);
}
}
public static string PopulateBody(string custFName, string custLName,
string checkoutDate, string dueDate, string bookName)
{
string body = string.Empty;
using (StreamReader reader =
new StreamReader(Path.GetFullPath(@"Z:\folder\email.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{#First Name#}", custFName);
body = body.Replace("{#Last Name#}", custLName);
body = body.Replace("{#Checkout Date#}", checkoutDate);
body = body.Replace("{#Due Date#}", dueDate);
body = body.Replace("{#Book Name#}", bookName);
return body;
}
public static void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mailMessage);
}
}
static void Main(string[] args)
{
SendEmail();
}
}
类程序
{
静态数据集dtProfile=Database.AcquireData();
静态数据表table=dtProfile.Tables[0];
静态字符串CustFName;
静态字符串CustLName;
静态字符串签出日期;
静态字符串日期;
静态字符串书名;
公共静态void sendmail()
{
foreach(table.Rows中的DataRow行)
{
CustFName=行[“CustFName”].ToString();
CustLName=行[“CustLName”].ToString();
CheckoutDate=行[“CheckoutDate”]。ToString();
DueDate=行[“DueDate”]。ToString();
BookName=行[“BookName”]。ToString();
string body=PopulateBody(CustFName、CustLName、CheckoutDate、DueDate、BookName);
SendHtmlFormattedEmail(“电子邮件”、“主题”、“正文”);
}
}
公共静态字符串PopulateBody(字符串custFName、字符串custLName、,
字符串签出日期、字符串dueDate、字符串bookName)
{
string body=string.Empty;
使用(StreamReader=
新的StreamReader(Path.GetFullPath(@“Z:\folder\email.html”))
{
body=reader.ReadToEnd();
}
body=body.Replace(“{#名字#}”,custFName);
body=body.Replace(“{Last Name}”,custLName);
body=body.Replace(“{Checkout Date}”,Checkout Date);
body=body.Replace(“{#到期日#}”,dueDate);
body=body.Replace(“{#图书名称}”,图书名称);
返回体;
}
公共静态void SendHtmlFormattedEmail(字符串recepientEmail、字符串主题、字符串正文)
{
使用(MailMessage=newmailmessage())
{
mailMessage.From=新邮件地址(ConfigurationManager.AppSettings[“用户名]);
mailMessage.Subject=主题;
mailMessage.Body=Body;
mailMessage.IsBodyHtml=true;
mailMessage.To.Add(新邮件地址(recepientEmail));
SmtpClient smtp=新SmtpClient();
smtp.Host=ConfigurationManager.AppSettings[“主机”];
smtp.EnableSsl=Convert.ToBoolean(ConfigurationManager.AppSettings[“EnableSsl”]);
System.Net.NetworkCredential NetworkCred=新系统.Net.NetworkCredential();
NetworkCred.UserName=ConfigurationManager.AppSettings[“UserName”];
NetworkCred.Password=ConfigurationManager.AppSettings[“Password”];
smtp.UseDefaultCredentials=true;
smtp.Credentials=NetworkCred;
smtp.Port=int.Parse(ConfigurationManager.AppSettings[“端口]);
smtp.Send(mailMessage);
}
}
静态void Main(字符串[]参数)
{
sendmail();
}
}
为每个电子邮件地址创建电子邮件数据集合。然后迭代各个地址并发送电子邮件
class Program
{
static DataSet dtProfile = null; //Database.AcquireData();
static DataTable table = dtProfile.Tables[0];
public static void SendEmail()
{
// Create the dictionary to hold the email data for each individual email. This allows us
// to group all of the books due for an individual together. We will use the email address
// as the key for the dictionary instead of CustomerID in case the user has given us two
// different email addresses.
Dictionary<string, List<DataRow>> emailList = new Dictionary<string, List<DataRow>>();
// Iterate over the dataset and populate the dictionary
foreach (DataRow row in table.Rows)
{
// grab the email address, will be the key for the dictionary
string email = row["Email"].ToString();
// if we haven't processed a row for this email yet, initialize the entry for it
if (!emailList.ContainsKey(email))
{
emailList.Add(email, new List<DataRow>());
}
// add the datarow for the overdue book for the email
emailList[email].Add(row);
}
// Now, craft and send an email for each unique email address in the list
foreach (string email in emailList.Keys)
{
// create a string builder to build up the body of the email
StringBuilder body = new StringBuilder();
body.Append("<html>");
body.Append("<body>");
// assume the first/last name will be the same for each row, so just get the
// name information from the first row to build the opening line of the email
DataRow firstRow = emailList[email][0];
body.AppendFormat("<p>Dear {0} {1}: The following book(s) are due:</p>", firstRow["FName"].ToString(), firstRow["LName"].ToString());
body.Append("<ol>");
// now just add a line item for each book
foreach (DataRow row in emailList[email])
{
body.AppendFormat("<li>{0}</li>", row["BookName"].ToString());
}
// close up your html tags
body.Append("</ol>");
body.Append("</body>");
body.Append("</html>");
// finally, send the email
SendHtmlFormattedEmail(email, "Overdue Books", body.ToString());
}
}
public static void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mailMessage);
}
}
static void Main(string[] args)
{
SendEmail();
}
}
类程序
{
静态数据集dtProfile=null;//数据库.AcquireData();
静态数据表table=dtProfile.Tables[0];
公共静态void sendmail()
{
//创建字典来保存每个电子邮件的电子邮件数据。这允许我们
//将个人到期的所有书籍分组在一起。我们将使用电子邮件地址
//作为字典的键,而不是CustomerID,以防用户给我们两个
//不同的电子邮件地址。
Dictionary emailList=新字典();
//迭代数据集并填充字典
foreach(table.Rows中的DataRow行)
{
//抓取电子邮件地址,将是字典的关键
字符串email=行[“email”]。ToString();
//如果我们尚未处理此电子邮件的行,请初始化该行的条目
如果(!emailList.ContainsKey(电子邮件))
{
添加(电子邮件,新列表());
}
//为电子邮件添加过期图书的数据行
emailList[电子邮件]。添加(行);
}
//现在,为列表中每个唯一的电子邮件地址制作并发送一封电子邮件
foreach(emailList.Keys中的字符串电子邮件)
{
//创建字符串生成器以构建电子邮件正文
StringBuilder主体=新的StringBuilder();
正文.附加(“”);
正文.附加(“”);
//假设每一行的名字/姓氏都相同,因此只需获取
//将第一行中的信息命名为电子邮件的开头行
DataRow firstRow=电子邮件列表[电子邮件][0];
body.AppendFormat(“亲爱的{0}{1}:以下书籍到期:”,第一行[“FName”].ToString(),第一行[“LName”].ToString());
正文.附加(“”);
//现在只需为每本书添加一行项目
foreach(emailList[电子邮件]中的数据行)
{
body.AppendFormat(“{0} ”,第[“BookName”].ToString()行);
}
//关闭你的html标签
正文.附加(“”);
正文.附加(“”);
正文.附加(“”);
//最后,发送电子邮件
SendHtmlFormattedEmail(电子邮件,“过期图书”,body.ToString());
}
}
公共静态void SendHtmlFormattedEmail(字符串recepientEmail、字符串主题、字符串正文)
{
使用(MailMessage=newmailmessage())
{
mailMessage.From=新邮件地址(ConfigurationManager.AppSettings[“Us
string lastCustId = "none";
foreach (DataRow row in table.Rows)
{
if (!lastCustId.Equals("none") && !row["CustId"].ToString().Equals(lastCustId))
{
// Different customer's record, fire off the email to previous one
SendHtmlFormattedEmail("Email", "Subject", body.ToString());
lastCustId = row["CustId"].ToString();
}
// Build email for current customer
CustFName = row["CustFName"].ToString();
CustLName = row["CustLName"].ToString();
CheckoutDate = row["CheckoutDate"].ToString();
DueDate = row["DueDate"].ToString();
BookName = row["BookName"].ToString();
body.AppendLine(PopulateBody(CustFName, CustLName, CheckoutDate, DueDate, BookName, template));
// not sure what your template looks like, but this would be whatever
// markup or text you would want separating your book details
body.AppendLine("<br>");
}
// Finally send email to the last customer in above loop
SendHtmlFormattedEmail("Email", "Subject", body.ToString());