Thursday, March 19, 2015

Send email to multiple users selected in gridview using asp.net (C#, VB)

Introduction: In this article I will explain how to send email to multiple users selected in gridview using asp.net (C#, VB)

Description:

Html Markup Of page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:GridView ID="Gridview1" runat="server">
<HeaderStyle Font-Bold="true" />
<Columns>
  <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="btnSend" Text="Send Mail" runat="server" OnClick="btnSend_Click" />
    </div>
    </form>
</body>
</html>


C# Code:
using System.Data;
using System.Net.Mail;
using System.Text;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    protected void BindGrid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(Int32));
        dt.Columns.Add("Student Name", typeof(string));
        dt.Columns.Add("Email", typeof(string));
         dt.Rows.Add(1, "Vijay", "saklanivijay87@gmail.com");
        dt.Rows.Add(2, "Vijay Saklani", "saklanivijay@ymail.com");
        dt.Rows.Add(3, "John", "john@gmail.com");
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
   
    protected void btnSend_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gridrow in Gridview1.Rows)
        {
           CheckBox chk = (CheckBox) gridrow.FindControl("chkSelect");
            if (chk.Checked==true)
            {          
            string studentname = gridrow.Cells[2].Text.Trim();
            string Email = gridrow.Cells[3].Text.Trim();
        
            StringBuilder sb = new StringBuilder();
            sb.Append("Hi  " + gridrow.Cells[2].Text.Trim());
            sb.Append(",<br/>");
            sb.Append("<br/>");
            sb.Append("<b>Welcome to Articlemirror</b><br/>");
            sb.Append("<br/>");
            sb.Append("<br/>");
            sb.Append("<b>Thanks</b>,<br> Articlemirror");

            string body = sb.ToString();
            Sendemail(Email,studentname,body);
            }
            chk.Checked = false;
        }
    }

    public void Sendemail(string To, string RecipientName, string body)
    {
        MailMessage mail = new MailMessage("saklanivijay87@gmail.com", To);
        mail.Subject = "Test Email"  + RecipientName;
        mail.Body = body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "password");
        smtp.EnableSsl = true;
        mail.IsBodyHtml = true;
        smtp.Send(mail);
    }

VB Code:
Imports System.Data
Imports System.Net.Mail
Imports System.Text

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub

    Protected Sub BindGrid()
        Dim dt As New DataTable()
        dt.Columns.Add("Id", GetType(Int32))
        dt.Columns.Add("Student Name", GetType(String))
        dt.Columns.Add("Email", GetType(String))
        dt.Rows.Add(1, "Vijay", "saklanivijay87@gmail.com")
        dt.Rows.Add(2, "Vijay Saklani", "saklanivijay@ymail.com")
        dt.Rows.Add(3, "John", "john@gmail.com")
        Gridview1.DataSource = dt
        Gridview1.DataBind()
    End Sub

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        For Each gridrow As GridViewRow In Gridview1.Rows
            Dim chk As CheckBox = DirectCast(gridrow.FindControl("chkSelect"), CheckBox)
            If chk.Checked = True Then
                Dim studentname As String = gridrow.Cells(2).Text.Trim()
                Dim Email As String = gridrow.Cells(3).Text.Trim()

                Dim sb As New StringBuilder()
                sb.Append("Hi  " + gridrow.Cells(2).Text.Trim())
                sb.Append(",<br/>")
                sb.Append("<br/>")
                sb.Append("<b>Welcome to Articlemirror</b><br/>")
                sb.Append("<br/>")
                sb.Append("<br/>")
                sb.Append("<b>Thanks</b>,<br> Articlemirror")

                Dim body As String = sb.ToString()
                Sendemail(Email, studentname, body)
            End If
            chk.Checked = False
        Next
    End Sub

    Public Sub Sendemail(ByVal [To] As String, ByVal RecipientName As String, ByVal body As String)
        Dim mail As New MailMessage("saklanivijay87@gmail.com", [To])
        mail.Subject = Convert.ToString("Test Email") & RecipientName
        mail.Body = body
        mail.IsBodyHtml = True
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.Port = 587
        smtp.Credentials = New System.Net.NetworkCredential("sender@gmail.com", "password")
        smtp.EnableSsl = True
        mail.IsBodyHtml = True
        smtp.Send(mail)
    End Sub

Result:-
Send email to multiple users selected in gridview

Send email to multiple users selected in gridview

Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment