插入数据后无法发送电子邮件更新

本文关键字:电子邮件 更新 数据 插入 | 更新日期: 2023-09-27 18:29:44

我正试图允许我的网络应用程序在数据插入数据库时发送电子邮件更新,就像下面显示的代码一样。

这是一个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();
                }

更糟糕的是,消息应该出现的标签没有出现。这意味着在插入数据之后,代码基本上停止运行。如果我在上面粘贴的代码令人困惑,我在这里的链接中添加了一个txtFile。

我真的仍然不明白为什么我的电子邮件在将数据插入数据库后没有运行。

谨致问候。

插入数据后无法发送电子邮件更新

您正在读取dr["email"].ToString(),但在select sql语句中只选择了assignto列。您可以更改select sql语句以同时选择assigntoemail列。