Thursday, December 24, 2020

Send Bulk Mails Using SMTP Configuration: Part 2

 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:

<body>

    <form id="form1" runat="server">

    <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>

        <br />

        <br />

        <asp:Button ID="Sendbtn" runat="server" Text="Send" OnClick="Sendbtn_Click"  />

        <asp:Label ID="cnfrm" runat="server"></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>

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:

protected void Sendbtn_Click(object sender, EventArgs e)

        {

            foreach (GridViewRow grow in GridView1.Rows)

            {

                string Emails = grow.Cells[0].Text.Trim();

 

                string file = Server.MapPath("~/Mail.html");

                string mailbody = System.IO.File.ReadAllText(file);

                string to = Emails;

                string from = "xyz@mail.com";

                MailMessage msg = new MailMessage(from, to);

                msg.Subject = "Auto Response Email";

                msg.Body = mailbody;

                msg.BodyEncoding = Encoding.UTF8;

                msg.IsBodyHtml = true;

                SmtpClient client = new SmtpClient("smtp.gmail.com", 25);

                System.Net.NetworkCredential basicCredential = new 
                System.Net.
NetworkCredential("xyz@mail.com""password");

                client.EnableSsl = true;

                client.UseDefaultCredentials = true;

                client.Credentials = basicCredential;

                try

                {

                    client.Send(msg);

                    cnfrm.Text = "Email Sended Successfully";

                }

                catch (Exception ex)

                {

                    Response.Write(ex.Message);

                }

            }

        }

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:

using System.Configuration;

using System.Text;

using System.Net;

using System.Net.Mail;

using System.Data;

using System.Data.OleDb;

 

namespace WebApplication64

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

        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();

        }

        protected void Sendbtn_Click(object sender, EventArgs e)

        {

            foreach (GridViewRow grow in GridView1.Rows)

            {

                string Emails = grow.Cells[0].Text.Trim();

 

                string file = Server.MapPath("~/Mail.html");

                string mailbody = System.IO.File.ReadAllText(file);

                string to = Emails;

                string from = "anubhavch689@gmail.com";

                MailMessage msg = new MailMessage(from, to);

                msg.Subject = "Auto Response Email";

                msg.Body = mailbody;

                msg.BodyEncoding = Encoding.UTF8;

                msg.IsBodyHtml = true;

                SmtpClient client = new SmtpClient("smtp.gmail.com", 25);

                System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz@gmail.com""password");

                client.EnableSsl = true;

                client.UseDefaultCredentials = true;

                client.Credentials = basicCredential;

                try

                {

                    client.Send(msg);

                    cnfrm.Text = "Email Sended Successfully";

                }

                catch (Exception ex)

                {

                    Response.Write(ex.Message);

                }

            }

        }

    }

}

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...