Social Icons

Thursday, July 24, 2014

C# - Send a Email in Asp.Net Using C#,


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();
}
}
}

No comments:

Post a Comment