Send confirmation Email after Approve request using C# and VB.Net in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

Hey,

In below code i want to send email to the user after approving and rejecting the request of the user by admin.

so please help me.

namespace ait0207_sem7_
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        protected void btn_approve_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString());
       
            foreach (GridViewRow row in Gv_approve.Rows)
            {
                CheckBox status = (row.Cells[3].FindControl("CheckBox1") as CheckBox);

                if (status.Checked)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("Update registration set status=1 where email= + '" + row.Cells[1].Text + "'", con);
                        cmd.CommandType = CommandType.Text;

                        cmd.Connection = con;

                        con.Open();
                        int suc = cmd.ExecuteNonQuery();
                        SqlDataReader dar = cmd.ExecuteReader();

                        if (suc > 0)
                        {
                            SendMail(row.Cells[1].Text);
                            Gv_approve.DataBind();
                            if (Gv_approve.Rows.Count == 0)
                            {
                                lbl_app.CssClass = "text-center text-primary";
                                lbl_app.Text = "All Users are approved ";
                                btn_approve.Visible = false;
                            }
                        }
                    }
                    catch
                    {                        

                    }
                    finally
                    {
                        if (con.State == ConnectionState.Open)
                            con.Close();
                    }
                }
            }
            lbl_app.CssClass = "text-center text-success";
            Response.Redirect("MIS_admin.aspx");
            lbl_app.Text = "Status Updated Successfully";
        }

        private void SendMail(string text)
        {
            throw new NotImplementedException();
        }

        protected void btn_rjct_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString());
            foreach (GridViewRow row in Gv_approve.Rows)
            {
                CheckBox status = (row.Cells[3].FindControl("CheckBox1") as CheckBox);

                if (status.Checked)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("delete from registration  where email= + '" + row.Cells[1].Text + "'", con);
                        cmd.CommandType = CommandType.Text;

                        cmd.Connection = con;

                        con.Open();
                        int suc = cmd.ExecuteNonQuery();

                        if (suc > 0)
                        {
                            SendMail(row.Cells[0].Text);
                            Gv_approve.DataBind();
                            if (Gv_approve.Rows.Count == 0)
                            {
                                lbl_app.CssClass = "text-center text-primary";
                                lbl_app.Text = "All Users are rejected ";
                                btn_approve.Visible = false;
                            }
                        }
                    }
                    catch
                    {                      
                    }
                    finally
                    {
                        if (con.State == ConnectionState.Open)
                            con.Close();
                    }
                }
            }
            lbl_app.CssClass = "text-center text-success";
            
            lbl_app.Text = "Status Updated Successfully";
        }
    }
}

 

 

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi nid@patel,

Refer below sample code.

HTML

<div class="container-fluid">
    <div class="col-sm-3">
        <%--menu panel--%>
        <asp:Panel ID="pnl_menu" runat="server" CssClass="panel-default">
            <div class="panel-heading" style="background-color: lightcoral">
                <h4 class="text-center">
                    <strong>Menu</strong></h4>
            </div>
            <div class="panel-body" style="background-color: cornflowerblue">
                <div class="col-sm-8">
                    <asp:Button ID="btn_Au" CssClass="btn btn-block btn-primary" runat="server" Text="Approve Users" />
                    <asp:Button ID="btn_Alu" CssClass="btn btn-block btn-primary" runat="server" Text="All User" />
                    <asp:Button ID="btn_state" CssClass="btn btn-block btn-primary" runat="server" Text="Manage States"
                        OnClick="btn_state_Click" />
                    <asp:Button ID="btn_dist" CssClass="btn btn-block btn-primary" runat="server" Text="Manage Districts"
                        OnClick="btn_dist_Click" />
                </div>
            </div>
        </asp:Panel>
    </div>
    <div class="col-sm-9">
        <asp:Panel ID="Panel_cntnt" CssClass="panel-default" runat="server">
            <div class="panel-heading" style="background-color: lightcoral">
                <h4>
                    <strong>Content</strong></h4>
            </div>
            <div class="panel-body">
                <asp:Panel ID="pnl_aut" runat="server">
                    <div class="panel-heading">
                        <h1 class="text-center">
                            Approve Users</h1>
                        <asp:Label ID="lbl_app" runat="server"></asp:Label>
                    </div>
                    <div class="panel-body">
                        <asp:GridView CssClass="table table-bordered table-striped table-hover" ID="Gv_approve"
                            runat="server" AutoGenerateColumns="False" CellPadding="6" DataSourceID="SqlDataSource1"
                            ForeColor="#333333" GridLines="None" Width="489px" DataKeyNames="Id">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <Columns>
                                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" InsertVisible="False"
                                    ReadOnly="True" />
                                <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
                                <asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" />
                                <asp:TemplateField HeaderText="Approve">
                                    <HeaderTemplate>
                                        <div class="text-center">
                                            <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />
                                            Approve/Reject
                                        </div>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="CheckBox1" runat="server" />
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                            </Columns>
                            <EditRowStyle BackColor="#999999" />
                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#E9E7E2" />
                            <SortedAscendingHeaderStyle BackColor="#506C8C" />
                            <SortedDescendingCellStyle BackColor="#FFFDF8" />
                            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                        </asp:GridView>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Mycon %>"
                            SelectCommand="SELECT [Id], [email],  [status] FROM [registration] WHERE ([status] = 0)">
                            <SelectParameters>
                                <asp:Parameter DefaultValue="0" Name="status" Type="String" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </div>
                    <asp:Button ID="btn_approve" CssClass="btn btn-default" runat="server" Text="Approve"
                        OnClick="btn_approve_Click" />
                    <asp:Button ID="btn_rjct" CssClass="btn btn-default" runat="server" Text="Reject"
                        OnClick="btn_rjct_Click" />
                </asp:Panel>
            </div>
        </asp:Panel>
    </div>
</div>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.Net;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Mail

Code

C#

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    CheckBox check = sender as CheckBox;
    if (check.Checked)
    {
        foreach (GridViewRow row in Gv_approve.Rows)
        {
            CheckBox status = (row.Cells[3].FindControl("CheckBox1") as CheckBox);
            status.Checked = true;
        }
    }
    else
    {
        foreach (GridViewRow row in Gv_approve.Rows)
        {
            CheckBox status = (row.Cells[3].FindControl("CheckBox1") as CheckBox);
            status.Checked = false;
        }
    }
}

protected void btn_approve_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString());
    foreach (GridViewRow row in Gv_approve.Rows)
    {
        CheckBox status = (row.Cells[3].FindControl("CheckBox1") as CheckBox);
        if (status.Checked)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("Update registration set status=1 where email= + '" + row.Cells[1].Text + "'", con);
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                int suc = cmd.ExecuteNonQuery();
                SqlDataReader dar = cmd.ExecuteReader();
                if (suc > 0)
                {
                    SendMail(row.Cells[1].Text);
                    Gv_approve.DataBind();
                    if (Gv_approve.Rows.Count == 0)
                    {
                        lbl_app.CssClass = "text-center text-primary";
                        lbl_app.Text = "All Users are approved ";
                        btn_approve.Visible = false;
                    }
                }
            }
            catch
            {

            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
        }
    }
    lbl_app.CssClass = "text-center text-success";
    Response.Redirect("MIS_admin.aspx");
    lbl_app.Text = "Status Updated Successfully";
}

private void SendMail(string recipient)
{
    MailMessage mm = new MailMessage("sender@gmail.com", recipient);
    mm.Subject = "Approval";
    mm.Body = "You are now active";
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential();
    NetworkCred.UserName = "sender@gmail.com";
    NetworkCred.Password = "<password>";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
}

protected void btn_rjct_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString());
    foreach (GridViewRow row in Gv_approve.Rows)
    {
        CheckBox status = (row.Cells[3].FindControl("CheckBox1") as CheckBox);

        if (status.Checked)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("delete from registration  where email= + '" + row.Cells[1].Text + "'", con);
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                int suc = cmd.ExecuteNonQuery();
                if (suc > 0)
                {
                    SendMail(row.Cells[1].Text);
                    Gv_approve.DataBind();
                    if (Gv_approve.Rows.Count == 0)
                    {
                        lbl_app.CssClass = "text-center text-primary";
                        lbl_app.Text = "All Users are rejected ";
                        btn_approve.Visible = false;
                    }
                }
            }
            catch
            {

            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }
    }
    lbl_app.CssClass = "text-center text-success";

    lbl_app.Text = "Status Updated Successfully";
}

protected void btn_state_Click(object sender, EventArgs e)
{
    Response.Redirect("MIS_Manage_States.aspx");
}

protected void btn_dist_Click(object sender, EventArgs e)
{
    Response.Redirect("MIS_Manage_District.aspx");
}

VB.Net

Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim check As CheckBox = TryCast(sender, CheckBox)
    If check.Checked Then
        For Each row As GridViewRow In Gv_approve.Rows
            Dim status As CheckBox = (TryCast(row.Cells(3).FindControl("CheckBox1"), CheckBox))
            status.Checked = True
        Next
    Else
        For Each row As GridViewRow In Gv_approve.Rows
            Dim status As CheckBox = (TryCast(row.Cells(3).FindControl("CheckBox1"), CheckBox))
            status.Checked = False
        Next
    End If
End Sub

Protected Sub btn_approve_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Mycon").ToString())
    For Each row As GridViewRow In Gv_approve.Rows
        Dim status As CheckBox = (TryCast(row.Cells(3).FindControl("CheckBox1"), CheckBox))
        If status.Checked Then
            Try
                Dim cmd As SqlCommand = New SqlCommand("Update registration set status=1 where email= + '" & row.Cells(1).Text & "'", con)
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                con.Open()
                Dim suc As Integer = cmd.ExecuteNonQuery()
                Dim dar As SqlDataReader = cmd.ExecuteReader()
                If suc > 0 Then
                    SendMail(row.Cells(1).Text)
                    Gv_approve.DataBind()

                    If Gv_approve.Rows.Count = 0 Then
                        lbl_app.CssClass = "text-center text-primary"
                        lbl_app.Text = "All Users are approved "
                        btn_approve.Visible = False
                    End If
                End If
            Catch
            Finally
                If con.State = ConnectionState.Open Then con.Close()
            End Try
        End If
    Next
    lbl_app.CssClass = "text-center text-success"
    Response.Redirect("MIS_admin.aspx")
    lbl_app.Text = "Status Updated Successfully"
End Sub

Private Sub SendMail(ByVal recipient As String)
    Dim mm As MailMessage = New MailMessage("sender@gmail.com", recipient)
    mm.Subject = "Approval"
    mm.Body = "You are now active"
    mm.IsBodyHtml = True
    Dim smtp As SmtpClient = New SmtpClient()
    smtp.Host = "smtp.gmail.com"
    smtp.EnableSsl = True
    Dim NetworkCred As NetworkCredential = New NetworkCredential()
    NetworkCred.UserName = "sender@gmail.com"
    NetworkCred.Password = "<password>"
    smtp.UseDefaultCredentials = True
    smtp.Credentials = NetworkCred
    smtp.Port = 587
    smtp.Send(mm)
End Sub

Protected Sub btn_rjct_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Mycon").ToString())
    For Each row As GridViewRow In Gv_approve.Rows
        Dim status As CheckBox = (TryCast(row.Cells(3).FindControl("CheckBox1"), CheckBox))
        If status.Checked Then
            Try
                Dim cmd As SqlCommand = New SqlCommand("delete from registration  where email= + '" & row.Cells(1).Text & "'", con)
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                con.Open()
                Dim suc As Integer = cmd.ExecuteNonQuery()
                If suc > 0 Then
                    SendMail(row.Cells(1).Text)
                    Gv_approve.DataBind()
                    If Gv_approve.Rows.Count = 0 Then
                        lbl_app.CssClass = "text-center text-primary"
                        lbl_app.Text = "All Users are rejected "
                        btn_approve.Visible = False
                    End If
                End If
            Catch
            Finally
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
            End Try
        End If
    Next
    lbl_app.CssClass = "text-center text-success"
    lbl_app.Text = "Status Updated Successfully"
End Sub

Protected Sub btn_state_Click(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("MIS_Manage_States.aspx")
End Sub

Protected Sub btn_dist_Click(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("MIS_Manage_District.aspx")
End Sub