电子邮件按钮单击Asp.Net中的事件

时间:2013-11-24 14:41:09

标签: asp.net email

我成功发送邮件,但我想要这样的电子邮件 docname:abc status = approve

我在桌子上放下了这个 table

现在在这里我想要当管理员点击提交按钮然后docname(如计划方法)和管理员选择的下拉值(如批准/ rejct / pending),例如他选择拒绝然后当用户收到邮件时他将能够看到像这个

docname:计划方法 状态:拒绝

这是按钮中的代码

 protected void Button1_Click(object sender, EventArgs e)
    {

            string connStr = 
             ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
          SqlConnection mySQLconnection = new SqlConnection(connStr);
          string empId = string.Empty;
          DataTable dt = new DataTable();

          if (mySQLconnection.State == ConnectionState.Closed)
          {
              mySQLconnection.Open();

              for (int i = 0; i < Repeater2.Items.Count; i++)
              {
                  DropDownList DropDownListcontrol = 
             ((DropDownList)Repeater2.Items[i].FindControl("DropDownList4"));

                  Label DocId = ((Label)Repeater2.Items[i].FindControl("DocId"));

                  SqlCommand cmd = new SqlCommand("approveddd", mySQLconnection);
                  cmd.CommandType = CommandType.StoredProcedure;


                  cmd.Parameters.Add("@DocID", SqlDbType.Int).Value = 
                 Convert.ToInt32((DocId.Text));

                  cmd.Parameters.Add("@ApproveID", SqlDbType.Int).Value = 
                Convert.ToInt32(DropDownListcontrol.SelectedValue);
                  cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = 
         (Session["Login2"]);




                  string DocName = 
          ((Label)Repeater2.Items[i].FindControl("DocName")).Text;
                  string emailId = 
               ((Label)Repeater2.Items[i].FindControl("YourEamil")).Text;
                  DropDownList dropdownvalue = 
               ((DropDownList)Repeater2.Items[i].FindControl("DropDownList4"));

                  string docname = String.Empty;
                  string emailID = String.Empty;
                  string dropdownvalues = String.Empty;

                  if (DocName.ToString() != "")
                  {
                      docname = DocName.ToString();
                  }
                  else
                  {
                      docname = "Unavailable";
                  }
                     if (emailId.ToString() != "")
                  {
                      emailID = emailId.ToString();
                  }
                  else
                  {
                      emailID = "Unavailable";
                  }

                  if (dropdownvalue.SelectedItem.ToString() != "")
                  {
                      dropdownvalues = dropdownvalue.SelectedItem.ToString();
                  }
                  else
                  {
                      dropdownvalues = "Unavailable";
                  }


                  SendEmailUsingGmail(DocName.ToString(), emailId.ToString(), 
           dropdownvalue.ToString());


                  cmd.ExecuteNonQuery();



              }

          }
          else
          {
              Supvisor.Text = "Error";
          }
          if (mySQLconnection.State == ConnectionState.Open)
          {
              mySQLconnection.Close();
          }

                }
    private void SendEmailUsingGmail(string DocName, string emailId, string
     dropdownvalue)
    {
        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new NetworkCredential("johkett@gmail.com", "123123120");
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            MailMessage message = new MailMessage();
            message.From = new MailAddress("johkett@gmail.com");
            message.To.Add(emailId);

            message.Subject = "DropDownList4 " + dropdownvalue;
            message.Body = "DocName=" + DocName + " DropDownList4=" + dropdownvalue;
            //message.Subject = "Write your email subject here";
            //message.Body = "write the content of the email here";
            smtp.Send(message);
        }
        catch (Exception ex)
        {
            Response.Write("Error occured: " + ex.Message.ToString());
        }
    }
像这样我想在电子邮件中看到 enter image description here

我是怎么做到的

1 个答案:

答案 0 :(得分:1)

您将控件作为字符串(dropdownvalues)传递,而不是传递dropdownvalue.ToString()

你应该更改这一行:

SendEmailUsingGmail(DocName.ToString(), emailId.ToString(), dropdownvalue.ToString());

对此:

SendEmailUsingGmail(DocName, emailId, dropdownvalues);

此外,我建议您删除不必要的ToString()表单。 `Button1_Click'应如下所示:

protected void Button1_Click(object sender, EventArgs e)
{
    string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
    SqlConnection mySQLconnection = new SqlConnection(connStr);
    string empId = string.Empty;
    DataTable dt = new DataTable();

    if (mySQLconnection.State == ConnectionState.Closed)
    {
        mySQLconnection.Open();

        for (int i = 0; i < Repeater2.Items.Count; i++)
        {
            DropDownList DropDownListcontrol = ((DropDownList)Repeater2.Items[i].FindControl("DropDownList4"));

            Label DocId = ((Label)Repeater2.Items[i].FindControl("DocId"));
            SqlCommand cmd = new SqlCommand("approveddd", mySQLconnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@DocID", SqlDbType.Int).Value = Convert.ToInt32((DocId.Text));
            cmd.Parameters.Add("@ApproveID", SqlDbType.Int).Value = Convert.ToInt32(DropDownListcontrol.SelectedValue);
            cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = (Session["Login2"]);

            string DocName = ((Label)Repeater2.Items[i].FindControl("DocName")).Text;
            string emailId = ((Label)Repeater2.Items[i].FindControl("YourEamil")).Text;
            DropDownList dropdownvalue = ((DropDownList)Repeater2.Items[i].FindControl("DropDownList4"));

            string docname = String.Empty;
            string emailID = String.Empty;
            string dropdownvalues = String.Empty;

            docname = !String.IsNullOrEmpty(DocName) ? DocName : "Unavailable";
            emailId = !String.IsNullOrEmpty(emailID) ? emailID : "Unavailable";
            dropdownvalues = !String.IsNullOrEmpty(dropdownvalue.SelectedValue) ? dropdownvalue.SelectedValue : "Unavailable";


            SendEmailUsingGmail(docname, emailId, dropdownvalues);
            cmd.ExecuteNonQuery();

        }

    }
    else
    {
        Supvisor.Text = "Error";
    }
    if (mySQLconnection.State == ConnectionState.Open)
    {
        mySQLconnection.Close();
    }

}

希望它有所帮助!