这是我的emailinfo表
> ID EMAIL CREDIT
> ----------------------------------- ----------
> 1 emal1@gmail.com 2500
> 2 email2@gmail.com 13500
> 3 email3@hotmail.com 3750
> 4 email4@outlook.com 11700
> 5 email5@outlook.com 1200
> 6 email6@yahoo.com 1997
> 7 email7@yahoo.com 179
> 8 emal8@gmail.com 10
我需要使用以下正文行将邮件发送到每个电子邮件地址,以告知她的信用值:
Email Body:Dear User your credit value: 2500
当我在代码下方执行时,每个电子邮件地址收到多封电子邮件,例如第一封电子邮件收到8封邮件,第二封电子邮件收到7封邮件,第三封电子邮件收到6封邮件,我如何避免收到多封电子邮件或如何轻松发送邮件而又不做任何处理错误如下代码:
MailMessage message = new MailMessage();
DataTable dataTable = new DataTable();
message.Subject = "Employee Access ";
message.From = new MailAddress("xyz@gmail.com");
var fromAddress = "zxy@gmail.com";
const string fromPassword="password";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
OleDbCommand cmd = null;
OleDbCommand cmd2 = null;
string queryString = "select id,email,credit from emailinfo";
using (OleDbConnection connection = new OleDbConnection("Provider = OraOLEDB.Oracle.1; Data Source = orcl; Password=bdipf;User ID = human; unicode=true"))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
cmd = new OleDbCommand(queryString);
cmd.Connection = connection;
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(dataTable);
adapter.Dispose();
}
foreach (DataRow dataRow in dataTable.Rows)
{
MailAddress to = new MailAddress(dataRow[2].ToString());
StringBuilder body = new StringBuilder();
if (!message.To.Contains(to))
{
message.To.Add(to);
message.Body = "Dear User your credit value: " + dataRow[2].ToString();
smtp.Send(message);continue
}
}
答案 0 :(得分:0)
最简单的解决方案是,您可以像下面的代码一样在foreach循环中创建MailMessage对象
DataTable dataTable = new DataTable();
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
OleDbCommand cmd = null;
OleDbCommand cmd2 = null;
string queryString = "select id,email,credit from emailinfo";
using (OleDbConnection connection = new OleDbConnection("Provider = OraOLEDB.Oracle.1; Data Source = orcl; Password=bdipf;User ID = human; unicode=true"))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
cmd = new OleDbCommand(queryString);
cmd.Connection = connection;
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(dataTable);
adapter.Dispose();
}
foreach (DataRow dataRow in dataTable.Rows)
{
MailMessage message = new MailMessage();
message.Subject = "Employee Access ";
message.From = new MailAddress("xyz@gmail.com");
var fromAddress = "zxy@gmail.com";
const string fromPassword="password";
message.To.Add(new MailAddress(dataRow[1].ToString()););
message.Body = "Dear User your credit value: " + dataRow[2].ToString();
smtp.Send(message);continue
}
}
您也可以在现有代码中进行较小的更改,并清除消息。要使用以下代码列出
foreach (DataRow dataRow in dataTable.Rows) {
MailAddress to = new MailAddress(dataRow[1].ToString());
message.To.clear();
message.To.Add(to);
message.Body = "Dear User your credit value: " + dataRow[2].ToString();
smtp.Send(message);
continue;
}