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.
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.
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.
<div>
<asp:Button ID="king" runat="server" Text="Show all the Members" OnClick="king_Click" />
<asp:Label ID="kinglbl" runat="server" Text="Total Members: "></asp:Label>
<asp:Label ID="Label2" runat="server" ForeColor="Red" Font-Size="Larger"></asp:Label>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</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:
using System.Configuration;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Data;
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:
protected void king_Click(object sender, EventArgs e)
{
OleDbConnection con=new OleDbConnection("provider=microsoft.ace.oledb.12.0;
data source=E:\\mail.xls;extended properties =excel 12.0");
con.Open();
OleDbCommand cmd = new OleDbCommand("Select Email,Name from [sheet1$]", con);
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Label2.Text = GridView1.Rows.Count.ToString();
con.Close();
}
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:
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:
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.