From admin side, I have list of registered users for my website... I want to send a mail to all listed users (or) want to send a mail to selected number of users from admin side.. Can you pleae help me out with the code to be use. I am a fresher, so please
bear with me if my questions are really dumb.
I have little bit idea about sending a confirmation mail to users when they have registered but I do not have idea of sending mail to multiple users and also the users mails should be picked from database.
I have a table for Registered Users with following columns " USER_ID, NAME, USERNAME,EMAIL, MOBILE No."
From Admin side I have a grid view which will list all these users and I have check box to Select few users also. GridVView Columns are
USERID,NAME,USERNAME,MAIL,MOBILE,SelectAll.
How to to pull email address data from database for selected users/all users and a send a mail to them. Please help me out. This is an urgent requirement ..Thanks in Advance..
For instance, lets say you have a default.aspx page which has a GridView => display all users, CheckBox => to select a user and SendEmail Button => to send an email to selected user. In order to send an email refer to code below:
protected void SendEmailBtn_Click(object sender, EventArgs e)
{
int gvRow = GridView.Rows.Count;
for (int i = 0; i < gvRow; i++)
{
CheckBox chkBox = (CheckBox)GridView.Rows[i].FindControl("selectEmailCheckBox");
if (chkBox.Checked == true)
{
var email = (Label)GridView.Rows[i].FindControl("emailLbl");
SendEmail(email.text);
}
}
GridView.DataBind();
}
protected void SendEmail(string emailId)
{
//TODO send email
}
hope this helps
Please "Mark as Answer" if this post answered your question.
I am using following snippet of code for the above requirement as per your suggestion. But I am getting an error like "can not implicitly convert type 'System.Web.UI.WebContols.Label' to 'String' "
protected void lnkbtnEmail_Click(object sender, EventArgs e)
{
int gvRow = gvUsers.Rows.Count;
for (int i = 0; i < gvRow; i++)
{
CheckBox chkBox = (CheckBox)gvUsers.Rows[i].FindControl("chkDelete");
if (chkBox.Checked == true)
{
string email = (Label)gvUsers.Rows[i].FindControl("Email ID");/*here is the error, you have casted to label but you havent used the Text property of it.*/
SendEmail(email);
}
}
gvUsers.DataBind();
}
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("setting");
mail.From = new MailAddress("a@b.com");
foreach (String recipient in recipients_ids)
{
mail.To.Add(recipient);
}
//where recipients_ids - string array which has collection of email ids
// other parameters
SmtpServer.Send(mail);
When a eye with Vision and a heart with passion achieves his dream people call him genius.
I have made changes as you suggested. Now i am getting another error like (NullReference Exception was unhandled by user code) Object reference not set to an instance of an object. at this particular line"string email = ((Label)gvUsers.Rows[i].FindControl("Email
ID")).Text ;"
protected void lnkbtnEmail_Click(object sender, EventArgs e)
{
int gvRow = gvUsers.Rows.Count;
for (int i = 0; i < gvRow; i++)
{
CheckBox chkBox = (CheckBox)gvUsers.Rows[i].FindControl("chkDelete");
if (chkBox.Checked == true)
{
string email = ((Label)gvUsers.Rows[i].FindControl("Email ID")).Text ;
SendMailTo(email);
}
harsharamoju
Member
4 Points
34 Posts
Send Email to Selected Users (or) To all users ....
Apr 11, 2012 01:22 AM|LINK
Hi,
From admin side, I have list of registered users for my website... I want to send a mail to all listed users (or) want to send a mail to selected number of users from admin side.. Can you pleae help me out with the code to be use. I am a fresher, so please bear with me if my questions are really dumb.
I have little bit idea about sending a confirmation mail to users when they have registered but I do not have idea of sending mail to multiple users and also the users mails should be picked from database.
I have a table for Registered Users with following columns " USER_ID, NAME, USERNAME,EMAIL, MOBILE No."
From Admin side I have a grid view which will list all these users and I have check box to Select few users also. GridVView Columns are
USERID,NAME,USERNAME,MAIL,MOBILE,SelectAll.
How to to pull email address data from database for selected users/all users and a send a mail to them. Please help me out. This is an urgent requirement ..Thanks in Advance..
2pac
Participant
1586 Points
269 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 05:41 AM|LINK
Hi,
For instance, lets say you have a default.aspx page which has a GridView => display all users, CheckBox => to select a user and SendEmail Button => to send an email to selected user. In order to send an email refer to code below:
protected void SendEmailBtn_Click(object sender, EventArgs e) { int gvRow = GridView.Rows.Count; for (int i = 0; i < gvRow; i++) { CheckBox chkBox = (CheckBox)GridView.Rows[i].FindControl("selectEmailCheckBox"); if (chkBox.Checked == true) { var email = (Label)GridView.Rows[i].FindControl("emailLbl"); SendEmail(email.text); } } GridView.DataBind(); } protected void SendEmail(string emailId) { //TODO send email }hope this helps
Regards,
Jayesh
harsharamoju
Member
4 Points
34 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 06:01 AM|LINK
Thanks a lot for the reply..
I am using following snippet of code for the above requirement as per your suggestion. But I am getting an error like "can not implicitly convert type 'System.Web.UI.WebContols.Label' to 'String' "
Code that I am using:
protected void lnkbtnEmail_Click(object sender, EventArgs e)
{
int gvRow = gvUsers.Rows.Count;
for (int i = 0; i < gvRow; i++)
{
CheckBox chkBox = (CheckBox)gvUsers.Rows[i].FindControl("chkDelete");
if (chkBox.Checked == true)
{
string email = (Label)gvUsers.Rows[i].FindControl("Email ID");
SendEmail(email);
}
}
gvUsers.DataBind();
}
2pac
Participant
1586 Points
269 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 06:07 AM|LINK
protected void lnkbtnEmail_Click(object sender, EventArgs e) { int gvRow = gvUsers.Rows.Count; for (int i = 0; i < gvRow; i++) { CheckBox chkBox = (CheckBox)gvUsers.Rows[i].FindControl("chkDelete"); if (chkBox.Checked == true) { string email = (Label)gvUsers.Rows[i].FindControl("Email ID");/*here is the error, you have casted to label but you havent used the Text property of it.*/ SendEmail(email); } } gvUsers.DataBind(); }modify it like this
var email = (Label)gvUsers.Rows[i].FindControl("Email ID"); SendEmail(email.Text); /*OR*/ string email = ((Label)gvUsers.Rows[i].FindControl("Email ID")).Text; SendEmail(email);Regards,
Jayesh
UjjwalMeshra...
Participant
1179 Points
293 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 06:10 AM|LINK
hi
MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("setting"); mail.From = new MailAddress("a@b.com"); foreach (String recipient in recipients_ids) { mail.To.Add(recipient); } //where recipients_ids - string array which has collection of email ids // other parameters SmtpServer.Send(mail);Regards
amitpatel.it
Star
7956 Points
1865 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 06:16 AM|LINK
Top up that ...
This code will help you to sent mail with template and where provision to set to list by adming in available list
http://amitpatelit.com/2011/04/11/sent-mail-c-code-by-template/
MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
harsharamoju
Member
4 Points
34 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 06:33 AM|LINK
Hi,
I have made changes as you suggested. Now i am getting another error like (NullReference Exception was unhandled by user code) Object reference not set to an instance of an object. at this particular line"string email = ((Label)gvUsers.Rows[i].FindControl("Email ID")).Text ;"
protected void lnkbtnEmail_Click(object sender, EventArgs e) { int gvRow = gvUsers.Rows.Count; for (int i = 0; i < gvRow; i++) { CheckBox chkBox = (CheckBox)gvUsers.Rows[i].FindControl("chkDelete"); if (chkBox.Checked == true) { string email = ((Label)gvUsers.Rows[i].FindControl("Email ID")).Text ; SendMailTo(email); }harsharamoju
Member
4 Points
34 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 06:38 AM|LINK
here is the code for grid view
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4" GridLines="None" AllowPaging="True" ForeColor="#333333" PageSize="20" > <Columns> <asp:BoundField HeaderText="ID" Visible="true" DataField="USER_ID" /> <asp:BoundField HeaderText="Name" DataField="FIRST_NAME" /> <asp:BoundField HeaderText="UserName" DataField="USER_NAME" /> <asp:BoundField HeaderText="Email ID" DataField="ALTERNATE_EMAIL" /> <asp:BoundField HeaderText="Mobile No" DataField="MOBILE_NO" /> <asp:TemplateField HeaderText="Delete"> <HeaderTemplate> <asp:CheckBox ID="chkAll" runat="server" onclick="javascript:fnCheckAll();" Text="Select" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkDelete" runat="server" /> </ItemTemplate> <HeaderStyle HorizontalAlign="center" /> <ItemStyle HorizontalAlign="center" /> </asp:TemplateField> </Columns> <FooterStyle BackColor="#9ACD47" ForeColor="White" Font-Bold="True" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#9ACD47" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#9ACD47" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <EditRowStyle BackColor="#2461BF" /> </asp:GridView>2pac
Participant
1586 Points
269 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 06:40 AM|LINK
remove the line below and use "TemplateField"
<asp:TemplateField HeaderText="Email Id"> <ItemTemplate> <asp:Label ID="EmailId" runat="server" Text='<%# Bind("ALTERNATE_EMAIL") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>Regards,
Jayesh
harsharamoju
Member
4 Points
34 Posts
Re: Send Email to Selected Users (or) To all users ....
Apr 11, 2012 08:40 AM|LINK
Thanks a lot it worked.