我想在电子邮件正文中插入数据库内容,数据阅读器的索引0包含每个电子邮件地址的ID号,我想使用在邮件正文中添加ID号来发送邮件,例如:雇员ID为5。 我需要在同一行中存在的每个邮件正文中插入每个id。 我已使用此代码在正文中插入ID,但这没用
message.Body = "The Employee id is 5 "+reader[1].ToString();
完整代码是:
MailMessage message = new MailMessage();
message.Subject = "Employee Access ";
message.From = new MailAddress("avvv@gmail.com");
var fromAddress = "avvv@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,status from tableemail";
using (OleDbConnection connection = new OleDbConnection("Provider = OraOLEDB.Oracle.1; Data Source = xe;
Password=654321;User ID = xpress; unicode=true"))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
cmd = new OleDbCommand(queryString);
cmd.Connection = connection;
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MailAddress to = new MailAddress(reader[1].ToString());
message.To.Add(to);
}
message.Body = "The Employee id is 5 "+reader[1].ToString();
smtp.Send(message);
reader.Close();
}
}
我可以使用数据读取器在正文中插入多个数据库字段吗?
答案 0 :(得分:0)
我不确定您的代码到底需要什么功能,但是据我了解,您可以这样做。使用DataTable
DataTable dataTable = new DataTable();
MailMessage message = new MailMessage();
message.Subject = "Employee Access ";
message.From = new MailAddress("avvv@gmail.com");
var fromAddress = "avvv@gmail.com";
const string fromPassword = "password";
OleDbCommand cmd = null;
string queryString = "select id,email,status from tableemail";
using (OleDbConnection connection = new OleDbConnection("Provider = OraOLEDB.Oracle.1; Data Source = xe; Password = 654321; User ID = xpress; 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)
{
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;
MailAddress to = new MailAddress(dataRow[1].ToString());
if (!message.To.Contains(to))
{
message.To.Add(to);
}
message.Body = "The Employee id is 5 " + dataRow[0].ToString();
smtp.Send(message);
}
}
答案 1 :(得分:0)
我认为问题是您在读取最后一条记录后正在调用DataReader。 Reader.Read()返回false,表示reader[1]
应该抛出错误,因为没有要读取的内容。
我不知道您是否想要电子邮件正文中的每个值,还是只想要最后一个...假设您想要每个值,类似的东西应该可以工作:
using (OleDbConnection connection = new OleDbConnection(cs))
{
connection.Open();
using (OleDbCommand cmd = new OleDbCommand(queryString, connection))
{
using (OleDbDataReader reader = cmd.ExecuteReader())
{
StringBuilder body = new StringBuilder();
while (reader.Read())
{
string to = reader.GetString(0);
message.To.Add(to);
body.AppendFormat("The Employee id is 5 {0}<br>", to);
}
message.Body = body.ToString();
reader.Close();
}
}
}
smtp.Send(message);
如果您真的只想要最后一个(正如您的原始代码所暗示的那样),我只需将其保存在循环中的本地字符串中,然后在阅读器循环之后将其添加。
在一个不相关的问题上,您将实例化OLE Command对象两次。我在上面的代码片段中删除了重复项。