Categories
Asp, Asp.net

Send Mail From Database In Batch Of 5 In Asp.Net

Send Mail From Database In Batch Of 5 In Asp.Net

In this tutorial we will learn how to send Send Mail From Database In Batch Of 5 In Asp.Net.In earlier post we have discussed about How To Send Multiple Emails From Database In ASP.NET Using C#.In this post we go one step ahead, means sending mails in a batch of 5 then wait for 60 seconds and then process the further list.

Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send Mail From Database In Batch Of 5 In Asp.Net</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<h2>Send Mail From Database In Batch Of 5 In Asp.Net</h2>
<div style="width:250px;margin:0px auto">

<br /><br /><br />
Subject: <asp:TextBox ID="txtsub" runat="server" Width="250px" Height="35px"></asp:TextBox><br /><br />
Message: <asp:TextBox ID="txtmsg" runat="server" Width="250px" Height="50px" TextMode="MultiLine"></asp:TextBox><br />
<asp:Button ID="send" Text="Send Mails" runat="server" OnClick="send_Click" />

<br /><br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</div> 
</form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Net.Mail;
using System.Security;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void send_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dev;User ID=sa;Password=hightech");
        con.Open();
        string str = "select * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        ArrayList emailArray = new ArrayList();

        while (reader.Read())
        {
            emailArray.Add(reader["uemail"]);
        }

        int count = 0;
        foreach (string email in emailArray)
        {
            const string username = "hightechnology@gmail.com";
            const string password = "hightech";
            SmtpClient smtpclient = new SmtpClient();
            MailMessage mail = new MailMessage();
            MailAddress fromaddress = new MailAddress("hightechnology@gmail.com");
            smtpclient.Host = "smtp.gmail.com";
            smtpclient.Port = 587;
            mail.From = fromaddress;
            mail.To.Add(email);
            mail.Subject = (txtsub.Text);
            mail.IsBodyHtml = true;
            mail.Body = "<html><head><title>" + HttpUtility.HtmlEncode(txtsub.Text) + "</title></head><body  style='height:600px'>" +
            "<p>Message: " + HttpUtility.HtmlEncode(txtmsg.Text) + "</p>" +
            "</body></html>";
            smtpclient.EnableSsl = true;
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
            try
            {
                smtpclient.Send(mail);
            }
            catch
            {

            }
            count++;
            if (count % 5 == 0)
            {
                System.Threading.Thread.Sleep(100000); // Wait for 10 seconds
            }
            lblMessage.Text = "Your email was sent";

        }
        reader.Close();
        con.Close();
    }
}