Thursday 6 June 2013

Send Bulk Emails using C# VB.NET in ASP.NET

Send Bulk Emails using C# VB.NET in ASP.NET 

http://www.codeshode.com/2011/07/send-bulk-emails-using-c-vbnet-in.html

However, I decided to post a basic "how to" for beginners so that they can easily understand the functionality of send bulk emails in ASP.NET.

For this example I will be using Northwind Database.

Step 1:





ASPX Code:

<form id="form1" runat="server">
    <div>
        Recipient(s):<br />
    <asp:TextBox ID="txtRecipient" runat="server" Height="50px" Width="525px"
            TextMode="MultiLine"></asp:TextBox>
        <br />
        <asp:Button ID="btnRecipientFromDB" runat="server"
            onclick="btnRecipientFromDB_Click" Text="Recipient From Database" />
        <br />
        <asp:CheckBoxList ID="chklstRecipientsFromDB" runat="server" Visible="False">
        </asp:CheckBoxList><asp:Button ID="btnAddSelected" runat="server"
            Text="Add Selected" onclick="btnAddSelected_Click" Visible="False" />
       
        <br />
        Subject:<br />
    <asp:TextBox ID="txtSubject" runat="server" Height="18px" Width="522px"></asp:TextBox>
   
        <br />
        <br />
        Email Body:<br />
        <asp:TextBox ID="txtEmailBody" runat="server" Height="290px" Width="520px"
            TextMode="MultiLine"></asp:TextBox>
        <br />
        <asp:Button ID="btnSend" runat="server" onclick="btnSend_Click"
            Text="Send Email" />
   
    </div>
</form>


This is simple form containing 3 TextBoxes

  1. Recipient(s)
  2. Email Subject
  3. Email Body
"Add Recipients from Database" button will show list of already added contacts from database as shown in image below:

Now, user has two options, either they can select multiple users from list and/or they can add recipients email address manually separated by ";"

Code Behind:
C#:

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

namespace TestApplication
{
    public partial class BulkEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                chklstRecipientsFromDB.Visible = false;
                btnAddSelected.Visible = false;
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                #region Direct Email to db Recipients
                //if you want to send emails directly to recipient from database, use code below
                //DataSet dsClients = GetClientDataSet();
                //if (dsClients.Tables["Users"].Rows.Count > 0)
                //{
                //    foreach (DataRow dr in dsClients.Tables["Users"].Rows)
                //    {
                //        SendEmail(dr["Email"].ToString());
                //    }
                //}
                #endregion

                char[] splitter = { ';' };
                foreach (string emailAdd in txtRecipient.Text.Split(splitter))
                {
                    SendEmail(emailAdd);
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnRecipientFromDB_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet dsClients = GetClientDataSet();

                chklstRecipientsFromDB.DataSource = dsClients.Tables[0];
                chklstRecipientsFromDB.DataTextField = "Email";
                chklstRecipientsFromDB.DataValueField = "Email";
                chklstRecipientsFromDB.DataBind();

                chklstRecipientsFromDB.Visible = true;
                btnAddSelected.Visible = true;
                btnRecipientFromDB.Visible = false;
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
        }
        ///
        ///  Creates and returns a DataSet using Ms Access OleDBConnection and an OleDBDataAdapter
        ///
        ///
        /// A DataSet from Ms Access using an OleDBConnection and an OleDBDataAdapter
        ///
        private System.Data.DataSet GetClientDataSet()
        {

            //retrieve the connection string from the ConnectionString Key in Web.Config
            //string connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Talha\Documents\Database2.accdb;Persist Security Info=False;";

            //create a new OleDB connection
            System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString);

            //pass the Select statement and connection information to the initializxation constructor for the OleDBDataAdapter
            System.Data.OleDb.OleDbDataAdapter myDataAdapter = new System.Data.OleDb.OleDbDataAdapter("SELECT Email FROM Employees", conn);

            //Create a new dataset with a table : CLIENTS
            System.Data.DataSet myDataSet = new System.Data.DataSet("Users");

            //Fill the dataset and table with the data retrieved by the select command
            myDataAdapter.Fill(myDataSet, "Users");

            //return the new dataset created
            return myDataSet;
        }
        private void SendEmail(string EmailAddress)
        {
            //Send Email Functionality here
            MailMessage mail = new MailMessage();
            mail.To.Add(EmailAddress);
            mail.From = new MailAddress("admin@codeshode.com");
            mail.Subject = txtSubject.Text;
            mail.Body = txtEmailBody.Text;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential("YourUserName@gmail.com", "YourGmailPassword");//Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }

        protected void btnAddSelected_Click(object sender, EventArgs e)
        {
            StringBuilder strRecipientEmails = new StringBuilder();
            foreach (ListItem chk in chklstRecipientsFromDB.Items)
            {
                if (chk.Selected)
                {
                    strRecipientEmails.Append(chk.Value);
                    strRecipientEmails.Append(";");
                }
            }
            txtRecipient.Text = strRecipientEmails.ToString();
            chklstRecipientsFromDB.Visible = false;
            btnAddSelected.Visible = false;
        }
    }
}

 

No comments:

Post a Comment