Send Bulk Mails Using SMTP Configuration: Part 2

Introduction

This is the second part of How to Send Bulk Mails Using SMTP Configuration.

In my previous article, "Send Bulk Mails Using SMTP Configuration- part 1", I have created an Excel Sheet in which more than 50 emails along with the name of users have been entered. Then I show them all in a Grid.

In this article I will enhance that application and show how to send an email to all users simply with a single button click.

Step 1

First I added a new button and a Label to the previous application, so the complete design code looks like this:

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.         <asp:Button ID="king" runat="server" Text="Show all the Members" OnClick="king_Click" />       
  5.         <asp:Label ID="kinglbl" runat="server" Text="Total Members: "></asp:Label>  
  6.         <asp:Label ID="Label2" runat="server" ForeColor="Red" Font-Size="Larger"></asp:Label>  
  7.         <br />  
  8.         <br />  
  9.         <asp:Button ID="Sendbtn" runat="server" Text="Send" OnClick="Sendbtn_Click"  />  
  10.         <asp:Label ID="cnfrm" runat="server"></asp:Label>  
  11.         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both">  
  12.             <AlternatingRowStyle BackColor="White" />  
  13.             <EditRowStyle BackColor="#7C6F57" />  
  14.             <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  15.             <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  16.             <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
  17.             <RowStyle BackColor="#E3EAEB" />  
  18.             <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
  19.             <SortedAscendingCellStyle BackColor="#F8FAFA" />  
  20.             <SortedAscendingHeaderStyle BackColor="#246B61" />  
  21.             <SortedDescendingCellStyle BackColor="#D4DFE1" />  
  22.             <SortedDescendingHeaderStyle BackColor="#15524A" />  
  23.         </asp:GridView>  
  24.     </div>  

You can see that I added a button whose Id is set to "Sendbtn" and a label whose Id is set to "cnfrm".

Now I will work on the Click Event of this button that is the main part of our complete application.

Step 2

Write this code in the .cs page:

  1. protected void Sendbtn_Click(object sender, EventArgs e)  
  2. {  
  3.     foreach (GridViewRow grow in GridView1.Rows)  
  4.     {  
  5.         string Emails = grow.Cells[0].Text.Trim();  
  6.         string file = Server.MapPath("~/Mail.html");  
  7.         string mailbody = System.IO.File.ReadAllText(file);  
  8.         string to = Emails;  
  9.         string from = "[email protected]";  
  10.         MailMessage msg = new MailMessage(from, to);  
  11.         msg.Subject = "Auto Response Email";  
  12.         msg.Body = mailbody;  
  13.         msg.BodyEncoding = Encoding.UTF8;  
  14.         msg.IsBodyHtml = true;  
  15.         SmtpClient client = new SmtpClient("smtp.gmail.com", 25);  
  16.         System.Net.NetworkCredential basicCredential = new   
  17.         System.Net.NetworkCredential("[email protected]""password");  
  18.         client.EnableSsl = true;  
  19.         client.UseDefaultCredentials = true;  
  20.         client.Credentials = basicCredential;  
  21.         try  
  22.         {  
  23.             client.Send(msg);  
  24.             cnfrm.Text = "Email Sended Successfully";  
  25.         }  
  26.         catch (Exception ex)  
  27.         {  
  28.             Response.Write(ex.Message);  
  29.         }  
  30.     }  
  31. } 

This code will work for each new row, in other words this code will check all the entries made in the Grid.

Since our emails are available in the first column, this code will fetch all the emails in a variable that is also named "Emails".

After this I took a variable named "file", in this variable the path of the file that is to be sent is provided, the next variable is named mailbody, this variable will read all the text available in the file whose path was provided in the previous variable.

The next two variables are named "to" and "from". The value of the first variable "Emails" is passed and in the second variable you need to pass your EmailID from which the email will be sent.

An object of SmtpClient is created and named "client", in the configuration port number is defined as "25" for sending the message from our local system. If we want to send the message from the server then the port number will be changed to "587".

Now our application is created and is ready for execution.

Output

On running the application you will get an output like this one:

Send Bulk Mail in ASP.NET

Here simply two buttons and a label can be seen.

Now when I click on the first button and output like this can be seen:

Send Bulk Mail in ASP.NET

Now I had clicked on the Send button and you can see I am getting the mail in my Gmail Account:

Send Bulk Mail in ASP.NET

You can also check the mail Id from where I had send the mail:

Send Bulk Mail in ASP.NET

The complete code of this application is as follows:

  1. using System.Configuration;  
  2. using System.Text;  
  3. using System.Net;  
  4. using System.Net.Mail;  
  5. using System.Data;  
  6. using System.Data.OleDb;  
  7. namespace WebApplication64  
  8. {  
  9.     public partial class WebForm1 : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.         }  
  14.         protected void king_Click(object sender, EventArgs e)  
  15.         {  
  16.             OleDbConnection con=new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=E:\\mail.xls;extended properties =excel 12.0");  
  17.             con.Open();  
  18.             OleDbCommand cmd = new OleDbCommand("Select Email,Name from [sheet1$]", con);  
  19.             OleDbDataAdapter adp = new OleDbDataAdapter(cmd);  
  20.             DataSet ds = new DataSet();  
  21.             adp.Fill(ds);  
  22.             GridView1.DataSource = ds;  
  23.             GridView1.DataBind();  
  24.             Label2.Text = GridView1.Rows.Count.ToString();  
  25.             con.Close();  
  26.         }  
  27.         protected void Sendbtn_Click(object sender, EventArgs e)  
  28.         {  
  29.             foreach (GridViewRow grow in GridView1.Rows)  
  30.             {  
  31.                 string Emails = grow.Cells[0].Text.Trim();  
  32.                 string file = Server.MapPath("~/Mail.html");  
  33.                 string mailbody = System.IO.File.ReadAllText(file);  
  34.                 string to = Emails;  
  35.                 string from = "[email protected]";  
  36.                 MailMessage msg = new MailMessage(from, to);  
  37.                 msg.Subject = "Auto Response Email";  
  38.                 msg.Body = mailbody;  
  39.                 msg.BodyEncoding = Encoding.UTF8;  
  40.                 msg.IsBodyHtml = true;  
  41.                 SmtpClient client = new SmtpClient("smtp.gmail.com", 25);  
  42.                 System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("[email protected]""password");  
  43.                 client.EnableSsl = true;  
  44.                 client.UseDefaultCredentials = true;  
  45.                 client.Credentials = basicCredential;  
  46.                 try  
  47.                 {  
  48.                     client.Send(msg);  
  49.                     cnfrm.Text = "Email Sended Successfully";  
  50.                 }  
  51.                 catch (Exception ex)  
  52.                 {  
  53.                     Response.Write(ex.Message);  
  54.                 }  
  55.             }  
  56.         }  
  57.     }  
  58. } 


Similar Articles