0

I just wanted to ask what is the best way to send email's on button click. My back end is in C# and front-end mainly in asp. I have simple table with few text boxes. I gather their values with jquery and send them via ajax to c# webmethod and then to database.

This is my front end part: https://jsfiddle.net/m8dLwf9z/85/

Ask you can see my button is pure html.

<input type="button" class="button" id="myButton" value="Save" />

This is my WebMethod where I take ajax values and send them to database with "Insert Object" stored procedure:

[WebMethod(EnableSession = true)]
        public static int GetCollection(List<myCollection> omyCollection, int ddl, string lvl)
        {   //Datatable is filled with foreach loop

        DataTable tableMain = new DataTable();
        tableMain.Columns.Add("CharID");
        tableMain.Columns.Add("CharaValue");

        foreach (myCollection mycol in omyCollection)
        {
            string label = mycol.label;
            string opis = mycol.opis;

            tableMain.Rows.Add(label, opis);

        }

        int success = 0;
        string conn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(conn))

            try
            {   
                //Check if values allready exist in the database 

                connection.Open();
                SqlCommand cmdCount = new SqlCommand("getDuplicate", connection);
                cmdCount.CommandType = CommandType.StoredProcedure;
                cmdCount.Parameters.AddWithValue("@ObjectName", lvl);


                var count = (string)cmdCount.ExecuteScalar();



                if (count == null)
                {
                    //Database insert
                    SqlCommand cmdProc = new SqlCommand("InsertObject", connection);
                    cmdProc.CommandType = CommandType.StoredProcedure;

                    //Parameters are sent to the database
                    cmdProc.Parameters.AddWithValue("@ObjectName", lvl);
                    cmdProc.Parameters.AddWithValue("@BCID", ddl);
                    cmdProc.Parameters.AddWithValue("@CharaValueParamether", tableMain);


                    cmdProc.ExecuteNonQuery();

                    //If the values are inserted send value 1 to ajax
                    success = 1;

                }

                else
                {

                }

            }

            catch
            {

            }
            finally
            {
                connection.Close();

            }

        return success;


    }

I think I have to have some webmethod c# code thats activated via ajax? Can someone give me some kind of help ?

Thanks in advance!

freej17
  • 361
  • 3
  • 12
  • 1
    look at this https://stackoverflow.com/questions/9201239/send-e-mail-via-smtp-using-c-sharp – Mihir Dave Jul 19 '18 at 13:27
  • 1
    Follow Mihir link to find how to use System.Net.Mail.SmtpClient to send mail. As far as how that works in your app - it could be invoked from your web method, or via a form post, or very simply using this technique https://www.w3schools.com/Html/tryit.asp?filename=tryhtml_form_mail – Brian Mains Jul 19 '18 at 13:34
  • 1
    It depends on many things, like how many emails you want to send, to how many people etc... That one time i needed functionality like this, I used SMTP -> [SMTP Configuration](https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx) server here's a link how to how to configure it. Here's another useful link [Step by step guide](http://csharp.net-informations.com/communications/csharp-smtp-mail.htm) – FilipYordanov Jul 19 '18 at 13:37
  • Thanks people! This solution worked perfectly ! :) – freej17 Jul 20 '18 at 08:36

0 Answers0