aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - How to Send Bulk Email in with Example using C#, VB.NET

Jul 3, 2014

here I will explain how to send bulk email in using c#, with example. To send mail to bulk people in we need to get all the user email ids and send mail to each emailid by using foreach loop in using c#,


In previous post I explained send html page as mail body in, send mail with images using gmail user credentials, send multiple attachments with email in,
how to send mail with attachment in and many more articles related to using c#, Now I will explain how to send bulk email in using c#, with example.

First create new web application and add one html page (HTML.htm) to your application in that write the following code


<html xmlns="">
<head><title>HTML Page to Send Mail </title></head>
<img src = "" /><br /><br />
<div style = "border-top:3px solid #EB5E00">&nbsp;</div>
<table style="border:1px solid #EB5E00">
<td><b><span style = "font-family:Arial;font-size:10pt">Admin:</span></b></td>
<td><b><span style = "font-family:Arial;font-size:10pt">CompanyName:</span></b></td>
<td><b><span style = "font-family:Arial;font-size:10pt">EMail:</span></b></td>
<td><b><span style = "font-family:Arial;font-size:10pt">Website:</span></b></td>
<p><span style = "font-family:Arial;font-size:10pt">To know more about, please visit -  </span> </p>
<span style = "font-family:Arial;font-size:10pt">Thanks</span>
<br />
<b><span style = "font-family:Arial;font-size:10pt">Aspdotnet-Suresh</span></b>

Now in your Default.aspx page write the following code

<html xmlns="">
<title>Send Bulk mail in using c#</title>
<form id="form1" runat="server">
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#DC5807" Font-Bold="true" ForeColor="White" />
<asp:Button ID="btnSend" Text="Send Mail" runat="server" OnClick="btnSend_Click" />
After completion of aspx page write the following code in codebehind

C# Code

using System;
using System.Data;
using System.IO;
using System.Net.Mail;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
/// <summary>
/// Dynamically create & bind data to gridview
/// </summary>
protected void BindGridviewData()
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Rows.Add(1, "Suresh Dasari", "");
dt.Rows.Add(2, "Rohini Dasari", "");
dt.Rows.Add(3, "Madhav Sai", "");
dt.Rows.Add(4, "Praveen", "");
dt.Rows.Add(6, "Sateesh", "");
dt.Rows.Add(7, "Mahesh Dasari", "");
dt.Rows.Add(8, "Mahendra", "");
gvDetails.DataSource = dt;
protected void btnSend_Click(object sender, EventArgs e)
foreach (GridViewRow grow in gvDetails.Rows)
string strContactName = grow.Cells[1].Text.Trim();
string strEmail = grow.Cells[2].Text.Trim();
StreamReader reader = new StreamReader(Server.MapPath("~/HTMLPage.htm"));
string readFile = reader.ReadToEnd();
string myString = readFile;
myString = myString.Replace("$$Admin$$", strContactName);
myString = myString.Replace("$$CompanyName$$", "Dasari Group");
myString = myString.Replace("$$Email$$", strEmail);
myString = myString.Replace("$$Website$$", "");
string from = "";
string to = strEmail;
MailMessage Msg = new MailMessage(from,to);
Msg.Subject = "Send Bulk mail for all users";
Msg.Body = myString;
Msg.IsBodyHtml = true;
string sSmtpServer = "";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;

Imports System.Data
Imports System.IO
Imports System.Net.Mail
Imports System.Web.UI.WebControls
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub

Protected Sub BindGridviewData()
Dim dt As New DataTable()
dt.Columns.Add("UserId", GetType(Int32))
dt.Columns.Add("UserName", GetType(String))
dt.Columns.Add("Education", GetType(String))
dt.Rows.Add(1, "Suresh Dasari", "")
dt.Rows.Add(2, "Rohini Dasari", "")
dt.Rows.Add(3, "Madhav Sai", "")
dt.Rows.Add(4, "Praveen", "")
dt.Rows.Add(6, "Sateesh", "")
dt.Rows.Add(7, "Mahesh Dasari", "")
dt.Rows.Add(8, "Mahendra", "")
gvDetails.DataSource = dt
End Sub
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each grow As GridViewRow In gvDetails.Rows
Dim strContactName As String = grow.Cells(1).Text.Trim()
Dim strEmail As String = grow.Cells(2).Text.Trim()
Dim reader As New StreamReader(Server.MapPath("~/HTMLPage.htm"))
Dim readFile As String = reader.ReadToEnd()
Dim myString As String = readFile
myString = myString.Replace("$$Admin$$", strContactName)
myString = myString.Replace("$$CompanyName$$", "Dasari Group")
myString = myString.Replace("$$Email$$", strEmail)
myString = myString.Replace("$$Website$$", "")
Dim from As String = ""
Dim [to] As String = strEmail
Dim Msg As New MailMessage(from, [to])
Msg.Subject = "Send Bulk mail for all users"
Msg.Body = myString
Msg.IsBodyHtml = True
Dim sSmtpServer As String = ""
Dim a As New SmtpClient()
a.Host = sSmtpServer
End Sub
End Class

Download Sample Code Attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

Failure sending mail. i am getting error when i am trying to sent a mail to group

Anonymous said...

if any mail Id is wrong ,message sending is failed. Plz Give me suggestion

Unknown said...

your code looks good just for 20-30 email ids,, but if use 500 or more emails then program shows time out as it takes a lot of time so that server displays time out error.
how we can get rid of this problem.?

Unknown said...

Above anonymous user, use can use jquery/ javascript code to check mail ids.
go n google for that

Jig said...

mail sending fails

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.