我正在尝试允许我的网络应用程序在数据插入数据库时发送电子邮件更新,就像我将在下面显示的代码一样。
这是一个btnAssign,它将使用数据
更新相关的数据库表和列protected void btnAssign_Click1(object sender, EventArgs e)
{
using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
String assign = ddlpid1.SelectedValue;
connAdd.Open();
var sql = "Update MemberReport Set assignto ='" + assign + "', caseprogress = 'ongoing' where memberreportID='" + lbmemberreportid.Text + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Insert into PoliceReport(memberreportid) values('" + lbmemberreportid.Text + "')";
// sql = "Update PoliceAccount Set handle ='" + assign + "' where policeid ='" + ddlpid1.SelectedValue + "' OR '" + ddlpid2.SelectedValue + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Update PoliceAccount Set handle ='" + lbmemberreportid.Text + "' where policeid ='" + ddlpid1.SelectedValue + "'";
// sql = "Update PoliceAccount Set handle ='" + assign + "' where policeid ='" + ddlpid1.SelectedValue + "' OR '" + ddlpid2.SelectedValue + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
}
数据库部分的插入/更新工作正常。当我添加smtp代码通过选择列发送电子邮件时,它没有工作。
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//con.Open();
// get the records matching the supplied username or email id.
cmd = new SqlCommand("select * from PoliceAccount where handle='" + lbmemberreportid.Text + "'", connAdd);
dr = cmd.ExecuteReader();
cmd.Dispose();
if (dr.HasRows)
{
dr.Read();
StringBuilder strBody = new StringBuilder();
//Passing emailid,username and generated unique code via querystring. For testing pass your localhost number and while making online pass your domain name instead of localhost path.
strBody.Append("<a>Please be notified that you've been assigned a case to handle. Please proceed to the scene with immediate effect.</a>");
// sbody.Append("&uCode=" + uniqueCode + "&uName=" + txtUserName.Text + ">Click here to change your password</a>");
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("apr13mpsip@gmail.com", dr["email"].ToString(), "Case Pending", strBody.ToString());
//pasing the Gmail credentials to send the email
System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("apr13mpsip@gmail.com", "Temasekpoly13");
System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
mailclient.EnableSsl = true;
mailclient.Credentials = mailAuthenticaion;
mail.IsBodyHtml = true;
mailclient.Send(mail);
dr.Close();
dr.Dispose();
cmd.ExecuteReader();
cmd.Dispose();
//con.Close();
lbmemberreportid.Text = "";
ddllocation.SelectedIndex = 0;
ddlnumber.SelectedIndex = 0;
ddlpid1.SelectedIndex = 0;
tbdetails.Text = "";
tbproperty.Text = "";
tbsuspect.Text = "";
ddlpid1.Visible = false;
LoadGrid();
lblmsg.ForeColor = System.Drawing.Color.Green;
lblmsg.Text = "MemberReportID" + Session["memberreportid"] + "has been successfully assigned";
}
connAdd.Close();
}
更糟糕的是,出现消息的标签没有出现。这意味着在插入数据后,代码基本上停止运行。如果上面粘贴的代码令人困惑,我在link添加了一个txtFile。
我仍然无法弄清楚为什么在将数据插入数据库后我的电子邮件无法运行。
问候。
答案 0 :(得分:1)
您正在阅读dr["email"].ToString()
,但只在select sql语句中选择assignto
列。您可以更改select sql语句以选择assignto
和email
列。