Thursday, March 19, 2015

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

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


Html Markup Of page:
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
<asp:GridView ID="Gridview1" runat="server">
<HeaderStyle Font-Bold="true" />
                <asp:CheckBox ID="chkSelect" runat="server" />
<asp:Button ID="btnSend" Text="Send Mail" runat="server" OnClick="btnSend_Click" />

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

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    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", "");
        dt.Rows.Add(2, "Vijay Saklani", "");
        dt.Rows.Add(3, "John", "");
        Gridview1.DataSource = dt;
    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("<b>Welcome to Articlemirror</b><br/>");
            sb.Append("<b>Thanks</b>,<br> Articlemirror");

            string body = sb.ToString();
            chk.Checked = false;

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

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
        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", "")
        dt.Rows.Add(2, "Vijay Saklani", "")
        dt.Rows.Add(3, "John", "")
        Gridview1.DataSource = dt
    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("<b>Welcome to Articlemirror</b><br/>")
                sb.Append("<b>Thanks</b>,<br> Articlemirror")

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

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

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