Send Bulk Email From Yahoo and Hotmail Using ASP.Net

Introduction

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:

  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; 

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:

  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.     <br />  
  6.     <br />  
  7.     <asp:Button ID="Sendbtn" runat="server" Text="Send" OnClick="Sendbtn_Click"  />  
  8.     <asp:Label ID="cnfrm" runat="server"></asp:Label>  
  9.     <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both">  
  10.         <AlternatingRowStyle BackColor="White" />  
  11.         <EditRowStyle BackColor="#7C6F57" />  
  12.         <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  13.         <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  14.         <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
  15.         <RowStyle BackColor="#E3EAEB" />  
  16.         <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
  17.         <SortedAscendingCellStyle BackColor="#F8FAFA" />  
  18.         <SortedAscendingHeaderStyle BackColor="#246B61" />  
  19.         <SortedDescendingCellStyle BackColor="#D4DFE1" />  
  20.         <SortedDescendingHeaderStyle BackColor="#15524A" />  
  21.     </asp:GridView>  
  22. </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:

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

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:

  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.mail.yahoo.com", 25);  
  16.         System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("[email protected]""YourPassword");  
  17.         client.EnableSsl = true;  
  18.         client.UseDefaultCredentials = true;  
  19.         client.Credentials = basicCredential;  
  20.         try  
  21.         {  
  22.             client.Send(msg);  
  23.             cnfrm.Text = "Email Sended Successfully";  
  24.         }  
  25.         catch (Exception ex)  
  26.         {  
  27.             Response.Write(ex.Message);  
  28.         }  
  29.     }  
  30. } 

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:

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


Similar Articles