6 Replies - 2652 Views - Last Post: 21 November 2013 - 11:50 AM Rate Topic: -----

#1 scropiogurl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 13

send email automatically in button click

Posted 21 November 2013 - 07:58 AM

previously i am trying to send mails in separate form
but now i want to send mail automatically in respective email address..

Admin approve/reject documents now i want to add
when admin approve/reject any documents then respective document name and the value which admin select (like approve /reject ) send to his/her mail ... previously i send mail using sepearte form through code is

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

        try
        {
            mySQLconnection.Open();
            for (int i = 0; i < Repeateremail.Items.Count; i++)
            {
 
                CheckBox checkboc =
                 ((CheckBox)Repeateremail.Items[i].FindControl("chkSelect"));
                if (checkboc != null)
                {
 
                    if (checkboc.Checked == true)
                    {
 
                        //get Current EMAIL_ID from the DataKey

                        string emailId =
                  ((Label)Repeateremail.Items[i].FindControl("lbl_email")).Text;
                        //write code to send mail
                        SendEmailUsingGmail(emailId);
                        dt.Clear();
                        dt.Dispose();
                    }
                    else if (checkboc.Checked == false)
                    {
                    }
                }
            }
 
        }
 

        catch (Exception ex)
        {
            emailsent.Text="Failed";
        }
        finally
        {
            empId = string.Empty;
        }
    }


in above code i send mail through check-boxes when admin check on checkbox and then click on send mail then mail send but now i want when admin approve document then this respective document name and value send in his/her mail .

My link

this is approve button code

protected void Button1_Click(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString; SqlConnection mySQLconnection = new SqlConnection(connStr); 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"]); cmd.ExecuteNonQuery(); //UPDATE APPPROVEID IN DOCUMENTINFO TABLE //DMSLIB.Doc myDoc = new DMSLIB.Doc(); //myDoc.MarkDocAs(Convert.ToInt16(DocId.Text), Convert.ToInt32(DropDownListcontrol.SelectedValue)); } } else { Supvisor.Text = "Error"; } if (mySQLconnection.State == ConnectionState.Open) { mySQLconnection.Close(); } }


this is approve button code

  protected void LinkButton1_Click(object sender, EventArgs e)
        {

            string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
            SqlConnection mySQLconnection = new SqlConnection(connStr);
            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"]);

                    cmd.ExecuteNonQuery();
                    //UPDATE APPPROVEID IN DOCUMENTINFO TABLE
                    //DMSLIB.Doc myDoc = new DMSLIB.Doc();
                    //myDoc.MarkDocAs(Convert.ToInt16(DocId.Text), Convert.ToInt32(DropDownListcontrol.SelectedValue));

                }

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


Is This A Good Question/Topic? 0
  • +

Replies To: send email automatically in button click

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8892
  • View blog
  • Posts: 33,347
  • Joined: 12-June 08

Re: send email automatically in button click

Posted 21 November 2013 - 08:01 AM

ASP.NET?
Was This Post Helpful? 0
  • +
  • -

#3 scropiogurl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 13

Re: send email automatically in button click

Posted 21 November 2013 - 09:58 AM

View Postmodi123_1, on 21 November 2013 - 08:01 AM, said:

ASP.NET?

yes in asp.net
Was This Post Helpful? 0
  • +
  • -

#4 scropiogurl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 13

Re: send email automatically in button click

Posted 21 November 2013 - 10:43 AM

i try to send mail in asp.net when admin approve /reject document then data save into database like this
SeqNo DocID ApproveID ApproveBy
82  20  3   john
83  21  1   john
84  18  2   kety
85  19  1   emel

now i send email also when admin click on button then email send to respective email id like this i show in repeater table
DocID DocName Uplaodedfile UserEmail       DocType DepType ApproveID
 1      ABC      def.pdf    abcdef@gmail.com  pdf      hr      (In this i set dropdown values are (approve/reject/pending)

and this is button code
protected void Button1_Click(object sender, EventArgs e)
    {
 
            string connStr = 
            ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
          SqlConnection mySQLconnection = new SqlConnection(connStr);
          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"]);
                  try
                  {
                      cmd.ExecuteNonQuery();
                      string emailId = 
              ((Label)Repeater2.Items[i].FindControl("Label2")).Text;
                      SendEmailUsingGmail(emailId);
                  }
                  catch (Exception ex)
                  {
                      Supvisor.Text=(ex.Message);
                  }
                  cmd.ExecuteNonQuery();
                  //UPDATE APPPROVEID IN DOCUMENTINFO TABLE
                  //DMSLIB.Doc myDoc = new DMSLIB.Doc();
                  //myDoc.MarkDocAs(Convert.ToInt16(DocId.Text), 
                 Convert.ToInt32(DropDownListcontrol.SelectedValue));
 
              }
 
          }
          else
          {
              Supvisor.Text = "Error";
          }
          if (mySQLconnection.State == ConnectionState.Open)
          {
              mySQLconnection.Close();
          }
                }
    private void SendEmailUsingGmail(string toEmailAddress)
    {
        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new NetworkCredential("keysketyyyy@gmail.com", 
             "sdsdasd");
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            MailMessage message = new MailMessage();
            message.From = new MailAddress("keysketyyy@gmail.com");
            message.To.Add(toEmailAddress);
            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());
        }
    }

error occured in this line when i set a breakpoint
catch (Exception ex)
        {
            Response.Write("Error occured: " + ex.Message.ToString());
        }

error

The parameter 'addresses' cannot be an empty string.
Parameter name: addresses
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8892
  • View blog
  • Posts: 33,347
  • Joined: 12-June 08

Re: send email automatically in button click

Posted 21 November 2013 - 10:52 AM

Do not create duplicate topics. Merging.
Was This Post Helpful? 0
  • +
  • -

#6 scropiogurl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 13

Re: send email automatically in button click

Posted 21 November 2013 - 10:56 AM

View Postmodi123_1, on 21 November 2013 - 10:52 AM, said:

Do not create duplicate topics. Merging.

i dont create duplicate topics i first i ask how i done this when i am trying then when i got a error thats why i posted here.

i dont create duplicate topics first i ask how i done this then when i am trying to do this then i got a error thats why i posted here.
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,441
  • Joined: 03-December 12

Re: send email automatically in button click

Posted 21 November 2013 - 11:50 AM

The Catch block doesn't cause the error, something in the try block before it did and sent you to the catch block.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1