Description:-
In this
Example we explain that How to Send Mail to Multiple User at a time Based on
CheckBox selection in Gridview in Asp.Net.
In previous Example we Explain only send Mail to one
user at a time but in this we provide Facility to Sending Email to Our Friend
by using our Application not Required Gmail you can Send Email From Your
Application Easily.
In which we provide Checkbox in Each row of Gridview
to select the user that you want to Send Email.we also provide Header CheckBox
to select All User Just Click on the Header CheckBox.
Here is Code for sending mail to multiple user
string
emailId = dt.Rows[0]["emailid"].ToString();
//write
code to send mail
SendEmailUsingGmail(emailId);
We Fetch all Email Id From Database that
you have to select From Gridview and send it to the SendEmailUsingGmail() Function like
private void
SendEmailUsingGmail(string toEmailAddress)
{
try
{
SmtpClient
smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("yougmail@gmail.com", "your gmailpassword");
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
MailMessage
message = new MailMessage();
message.From = new MailAddress("kiritkapupara24@gmail.com");
message.To.Add(toEmailAddress);
message.Subject = "I
am Sending this Mail From My application";
message.Body = "so
Enjoyed it self.";
smtp.Send(message);
}
catch(Exception
ex)
{
Response.Write("Error
occured: " + ex.Message.ToString());
}
}
This Function will send the mail that
you have select From Gridview one by one.
Here is the Output of this Progarm are as Follow:
To send SMS in Asp.Net to any User then click Here Send SMS or message in Asp.net
To Send Email in Asp.Net click Here Send Email to Other user in ASP.net
To send Email with Attechment File click Here Send Email with Atteched File in Asp.Net
To Import Gmail Contact in Asp.Net click here Import contact from Gmail in asp.net
To send Email to Multiple User at a time based on checkbox selection click here Send Email to Multiple User in Asp.net
To Send Forgot to an Email in Asp.Net click here Send Forgot Password to user's Email
sendmailtomultipleuser.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sendmailtomultipleuser.aspx.cs" Inherits="sendmailtomultipleuser" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:515px;">
<legend><b>Send
Mail to multiple users based on CheckBox Selection inside GridView</b></legend>
<table border="1">
<tr>
<td>
<asp:GridView ID="grEmp" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="id" GridLines="None" Width="100%" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="id" HeaderText="id" Visible="False" />
<asp:BoundField DataField="NAME" HeaderText="Name" />
<asp:BoundField DataField="Question" HeaderText="Question" />
<asp:BoundField DataField="emailid" HeaderText="Email
Id" />
<asp:TemplateField HeaderText="CheckAll">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkSelectAll_CheckedChanged"/>Send
Mail To All ?
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server"/>
</ItemTemplate>
</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" />
</asp:GridView>
</td>
</tr>
</table>
<asp:Button ID="btnSendMail" runat="server" Text="Send
Email" OnClick="btnSendMail_Click" />
</fieldset>
</div>
</form>
</body>
</html>
sendmailtomultipleuser.aspx.cs:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Net;
using
System.Net.Mail;
using
System.Data;
using
System.Data.SqlClient;
public partial class sendmailtomultipleuser :
System.Web.UI.Page
{
SqlConnection
con = new SqlConnection("Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@");
protected void
Page_Load(object sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
Bindgrid();
}
}
protected void
Bindgrid()
{
SqlCommand
cmd = new SqlCommand("select
* from sendmail", con);
DataTable dt
= new DataTable();
SqlDataAdapter
adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
grEmp.DataSource = dt;
grEmp.DataBind();
}
protected void
btnSendMail_Click(object sender, EventArgs e)
{
string
userid = string.Empty;
DataTable dt
= new DataTable();
try
{
foreach (GridViewRow
row in grEmp.Rows)
{
CheckBox cb
= (CheckBox)row.FindControl("chkSelect");
if
(cb.Checked == true)
{
if
(cb != null && cb.Checked)
{
//get
Current EMAIL_ID from the DataKey
userid = Convert.ToString(grEmp.DataKeys[row.RowIndex].Value);
SqlCommand
cmd = new SqlCommand("select
emailid from sendmail where id=" + userid + "",
con);
SqlDataAdapter
adp = new SqlDataAdapter(cmd);
//Fill
datatable with EMAIL_ID corresponding to Current EMP_ID
adp.Fill(dt);
//Get
EMAIL_ID into variable
string
emailId = dt.Rows[0]["emailid"].ToString();
//write
code to send mail
SendEmailUsingGmail(emailId);
dt.Clear();
dt.Dispose();
}
}
}
ScriptManager.RegisterClientScriptBlock(Page,
Page.GetType(), Guid.NewGuid().ToString(), "alert('Emails
sent successfully');", true);
}
catch (Exception
ex)
{
Response.Write("Error
occured: " + ex.Message.ToString());
}
finally
{
userid = string.Empty;
}
}
private void
SendEmailUsingGmail(string toEmailAddress)
{
try
{
SmtpClient
smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("your Email@gmail.com", "your gmail password");
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
MailMessage
message = new MailMessage();
message.From = new MailAddress("kiritkapupara24@gmail.com");
message.To.Add(toEmailAddress);
message.Subject = "I
am Sending this Mail From My application";
message.Body = "so
Enjoyed it self.";
smtp.Send(message);
}
catch(Exception
ex)
{
Response.Write("Error
occured: " + ex.Message.ToString());
}
}
protected void
chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox
chkAll =
(CheckBox)grEmp.HeaderRow.FindControl("chkSelectAll");
if
(chkAll.Checked == true)
{
foreach (GridViewRow
gvRow in grEmp.Rows)
{
CheckBox
chkSel =
(CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = true;
}
}
else
{
foreach (GridViewRow
gvRow in grEmp.Rows)
{
CheckBox
chkSel = (CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = false;
}
}
}
}
how multiple user send there email to a singleuser.or
ReplyDeletemultipleclients sending there email to a comapny.
here in this case a single user sending email to multiple user by providing his email id and password,when multiple user send mail to a single user how to do this.
ReplyDeleteone user can send email to multiple user but multiple user can not send email to one user at a time.to do this you have to set email one by one at a rume time or dynamic like
ReplyDeletesmtp.Credentials = new NetworkCredential("your Email@gmail.com", "your gmail password");
change the email id and password in foreach loop one send them to one user simply... thanks for comment if you have any problem then plz put your comment
thanks for ur reply,but still i am in doubt,
Deletesuppose: one example:
i want to buy book from a bookshop. its online.
the fields are:
name:
customer email:
message:
i have mention my name:xxx
email:xxx@gmail.com
message:i want to buy c# book.
another client come to the shop
he also enter his name,email,message.
the shop receiving our emails and send us a confirmation message like thanks for showing intrest,like this..
how is this possible in c#.net.
any reply or any help is highly appreciated.
with thanks.
Simply get the email address and send the Email that user enter at a time while buying a book
DeleteHi. I entered the codes exactly like yours but it just won't go through. May I ask where do you get the ID from: where id=" + userid + ?
ReplyDeleteYour help is greatly appreciated!