-->

Friday, July 4, 2014

How to send bulk email in asp.net

How to send bulk email in asp.net

If you want to send email in bulk then you should go for my algorithm. I have already learn about SMTP services (how to to send e-mail). Today we will learn , how to send email in bulk. There are various steps to send email in bulk, these are
1. Bind proper information of customer in gridview (learn how to bind gridview in asp.net)


2. Make user friendly application, in which you can insert data in gridview at runtime.(How to insert data into database)
3. Take two textboxes on design window for email-subject and email-message.
4. Also add single button control for sending message.
5. Raise click_event for button control.
6. Run foreach loop for all rows of gridview like

foreach (GridViewRow  gdata in GridView1 .Rows)
        {
}
7. Extract e-mail field from gridview, now, your code look like.

 string email = gdata.Cells[3].Text.Trim();

8. After retrieving single address from gridview, now you can send message.

9. Run your application.


Complete 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.Configuration;
using System.Net.Mail;

public partial class daclass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString();
        con.Open();
        SqlCommand cmd=new SqlCommand ();
        cmd.CommandText ="select * from [user]";
        cmd.Connection =con;
        SqlDataAdapter da=new SqlDataAdapter (cmd);
        DataSet ds=new DataSet();
        da.Fill (ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();          
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow  gdata in GridView1 .Rows)
        {
            string email = gdata.Cells[3].Text.Trim();
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("your gmail id here");
            msg.To.Add(email);
            msg.Subject = TextBox1.Text;
            msg.Body = TextBox2.Text;
            msg.IsBodyHtml = true;
            SmtpClient smt = new SmtpClient("smtp.gmail.com", 587);
            smt.EnableSsl = true;
            smt.Credentials = new System.Net.NetworkCredential("your gmail id here", "your gmail password");
            smt.Send(msg);
            Response.Write("message send");
            
        }
    }
}

Code Generate the following output
How to send bulk email in asp.net

How to send bulk email in asp.net

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved