Aspdotnet-Suresh

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

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

Jul 3, 2014
Introduction:

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

Description:

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

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

HTML.htm


<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>HTML Page to Send Mail </title></head>
<body>
<img src = "https://lh5.googleusercontent.com/_B28NJpJ61hA/TdgnS7lh7mI/FinalLogo.png" /><br /><br />
<div style = "border-top:3px solid #EB5E00">&nbsp;</div>
<table style="border:1px solid #EB5E00">
<tr>
<td><b><span style = "font-family:Arial;font-size:10pt">Admin:</span></b></td>
<td>$$Admin$$</td>
</tr>
<tr>
<td><b><span style = "font-family:Arial;font-size:10pt">CompanyName:</span></b></td>
<td>$$CompanyName$$</td>
</tr>
<tr>
<td><b><span style = "font-family:Arial;font-size:10pt">EMail:</span></b></td>
<td>$$Email$$</td>
</tr>
<tr>
<td><b><span style = "font-family:Arial;font-size:10pt">Website:</span></b></td>
<td>$$Website$$</td>
</tr>
</table>
<p><span style = "font-family:Arial;font-size:10pt">To know more about Aspdotnet-Suresh.com, please visit - http://www.aspdotnet-suresh.com  </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>
</body>
</html>

Now in your Default.aspx page write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Send Bulk mail in asp.net using c#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#DC5807" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
<asp:Button ID="btnSend" Text="Send Mail" runat="server" OnClick="btnSend_Click" />
</form>
</body>
</html>
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)
{
BindGridviewData();
}
/// <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", "sureshbabudasari@gmail.com");
dt.Rows.Add(2, "Rohini Dasari", "rohini@gmail.com");
dt.Rows.Add(3, "Madhav Sai", "madhav@gmail.com");
dt.Rows.Add(4, "Praveen", "praveen@hotmail.com");
dt.Rows.Add(6, "Sateesh", "sateesh@yahoo.com");
dt.Rows.Add(7, "Mahesh Dasari", "maheshd@gmail.com");
dt.Rows.Add(8, "Mahendra", "sureshbabudasari@hotmail.com");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
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$$", "http://www.aspdotnet-suresh.com");
string from = "administrator@aspdotnet-suresh.com";
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 = "10.82.42.122";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.Send(Msg);
reader.Dispose();
}
}
}
VB.NET Code


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
BindGridviewData()
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", "sureshbabudasari@gmail.com")
dt.Rows.Add(2, "Rohini Dasari", "rohini@gmail.com")
dt.Rows.Add(3, "Madhav Sai", "madhav@gmail.com")
dt.Rows.Add(4, "Praveen", "praveen@hotmail.com")
dt.Rows.Add(6, "Sateesh", "sateesh@yahoo.com")
dt.Rows.Add(7, "Mahesh Dasari", "maheshd@gmail.com")
dt.Rows.Add(8, "Mahendra", "sureshbabudasari@hotmail.com")
gvDetails.DataSource = dt
gvDetails.DataBind()
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$$", "http://www.aspdotnet-suresh.com")
Dim from As String = "administrator@aspdotnet-suresh.com"
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 = "10.82.42.122"
Dim a As New SmtpClient()
a.Host = sSmtpServer
a.Send(Msg)
reader.Dispose()
Next
End Sub
End Class
Demo


Download Sample Code Attached


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

Get the latest Asp.net, C#.net, 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

5 comments :

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 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.