Thursday, December 24, 2020

Send Bulk Email From Yahoo and Hotmail Using ASP.Net

 In my previous article I told you about How to Send Bulk Mail Using SMTP Configuration. In that article I sent the emails using Gmail credentials, if you try to send an email using your Yahoo or Hotmail credential then an error message will be shown because you need a different configuration for both Yahoo and Hotmail.

In this article I will tell you about how to send bulk email from Yahoo and Hotmail using ASP.NET.

Step 1

First of all you need to add some namespaces to your application, which are as follows:

using System.Configuration;

using System.Text;

using System.Net;

using System.Net.Mail;

using System.Data;

using System.Data.OleDb;

Step 2

After adding these namespaces you need to add a Grid View and a button to your application, write this code in the .aspx page:

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

Here I took two buttons, the click of the first button will show the person's name and their MailIds in the Grid View, the click of the second button will send the mail.

Step 3

Now you need to create a database of people to whom email needs to be sent, you can create the database in SQL, Access, Excel, Oracle or any other database depending on your needs. The main thing is you must know how to bind them with an ASP.NET application and how to show their data in a Grid. I have created an Excel Sheet in which more then 60 users have been entered.

After creating the database, now it's time to bind it to our application, write this code on the click of the first button:

        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 simply created a connection in which the path of the Excel Sheet is provided, after that I fetched the name and Email Id of the users and then shown them in a Grid.

Step 4

Now I will show how to send email using Yahoo Credentials, write this code on the click of the second button:

        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@yahoo.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.mail.yahoo.com", 25);

                System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz@yahoo.com""YourPassword");

                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 row in the Grid.

Here I took a variable whose name is Emails, in this variable the first column of our Excel sheet is bound; that's because in the first column I provided the Email Id of the users.

After this the path of the file whose content is to be sent in all the mails is provided in a variable named file.

After this all the Email Id are passed into "to" and your mail Id can be passed into "from".

Now the main thing is "SmtpClient", while we were working on a Gmail account we have used SmtpClient="smtp.gmail.com" but for Yahoo it is changed to "smtp.mail.yahoo.com". This is the thing that will help you send the mail using Yahoo credentials.

The Port number is "25", if you are using this application on your server and not locally then the port number will changed to 587.

After this you need to provide your Yahoo Mail Id and it's password.

Step 5

If you want to send email using a Hotmail Account then SmtpClient should be "smtp.live.com".

Now our application is ready for execution.

Output

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

Send Bulk Email from Yahoo and Hotmail Using ASP.NET

Here simply two buttons and a label can be seen.

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

Send Bulk Email from Yahoo and Hotmail Using ASP.NET

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

Send Bulk Email from Yahoo and Hotmail Using ASP.NET

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

Send Bulk Email from Yahoo and Hotmail Using ASP.NET

The complete code of this application is as follows:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

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 = "xyz@yahoo.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.mail.yahoo.com", 25);

                System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz@yahoo.com""YourPassword");

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