Send Bulk Mails Using SMTP Configuration: Part 1

Introduction

Now I will create two articles whose combination will create an interesting application, in other words send bulk mails using SMTP Configuration.

This is the first article of this two article series and in this article I will create an Excel Sheet in which more than 50 users data will be inserted, then this data will be shown in a grid.

The next/final  Part can be seen here:  Send Bulk Mails Using SMTP Configuration: Part 2.

We often need to send an email to multiple users, in those cases we forward the email again and again but using this application we can send the email to all the users available in an Excel Sheet by a single click.

Use the following procedure to create such an application.

Step 1

First of all I created an Excel Sheet in which I made some entries in two columns, in other words Email and Name.

Send Bulk Mail Using ASP.NET

I am sending all the mails to my other EmailId, you can send it to various Ids.

Step 2

Now I have created a new ASP.NET application in which a button, label and a Grid is used.

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

All these controls have their specific functions as in the following:

  • Click of the button will fetch the data
  • Label will show the number of Members available
  • Grid will show the complete information that is available in the Excel Sheet

Step 3

After this I provided the coding that will work for us.

First of all you need to add these namespaces to your application:

  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;   

After adding these namespaces you need to work on the click of button, add this code for the button click event:

  1. protected void king_Click(object sender, EventArgs e)  
  2. {  
  3.     OleDbConnection con=new OleDbConnection("provider=microsoft.ace.oledb.12.0;  
  4.     data source=E:\\mail.xls;extended properties =excel 12.0");  
  5.     con.Open();  
  6.     OleDbCommand cmd = new OleDbCommand("Select Email,Name from [sheet1$]", con);  
  7.     OleDbDataAdapter adp = new OleDbDataAdapter(cmd);  
  8.     DataSet ds = new DataSet();  
  9.     adp.Fill(ds);  
  10.     GridView1.DataSource = ds;  
  11.     GridView1.DataBind();  
  12.     Label2.Text = GridView1.Rows.Count.ToString();  
  13.     con.Close();  
  14. } 

Here I provided the connection with the Excel Sheet that is saved in my E Drive.

After opening the connection I called all the values of Email and Name from Sheet1.

Then I bound the Grid to the data available.

At the end I had shown the number of rows to the Label Text, in other words it will show the number of rows available.

Now if you run the application then something like this will be shown:

bulkmail

First a button and a label will be shown.

If I click on the button then the Label will show the total number of members available in the Sheet, also a Grid of all the users will also be shown as in the following:

Send Bulk Mail Using ASP.NET

Now our first part of this application is created. In our next part or in the next article you will see How to send Mails Using SMTP Configurations.


Similar Articles