Send email to each email address in database

28 Dec

Some time ago I need something that read a email column in the database and send a generic message to the total amount of emails.

After seen this reference blog:

Everything works great, I’m going to supply the code because some small things was change.

Note: The example was done in C#, I’m assuming that you create a C# web Form that create some default code.

Create this two controls:

<form id=”form1″ runat=”server”>
<asp:TextBox ID=”BodyTextBox” runat=”server”></asp:TextBox><br />
<asp:Button ID=”btnSendEmail” runat=”server” Text=”Button” OnClick=”btnSendEmail_Click” />


using System.Data.SqlClient;
using System.Net.Mail;

protected void btnSendEmail_Click(object sender, EventArgs e)
string strConn = ConfigurationManager.ConnectionStrings[“cs”].ConnectionString ;
using (SqlConnection objConn = new SqlConnection(strConn))
SqlCommand comEmailAddresses = new SqlCommand(“SELECT email from ee_email”, objConn);
using (SqlDataReader rdrEmailAddresses = comEmailAddresses.ExecuteReader())
while (rdrEmailAddresses.Read())
string emailAddress = (rdrEmailAddresses[“email”] is DBNull ? null : (string)rdrEmailAddresses[“email”]);
if (emailAddress != null)
bool success = false;
success = SendEmail(emailAddress);
if (!success)
//Handle this scenario if necessary
//Handle this scenario if necessary

private bool SendEmail(string emailAddress)

MailAddress SendFrom = new MailAddress(““, “User Name“);
MailAddress SendTo = new MailAddress(emailAddress);
MailMessage Message = new MailMessage(SendFrom, SendTo);
Message.Subject = “Test email”;
Message.IsBodyHtml = true;
Message.Body = BodyTextBox.Text;
SmtpClient MessageSMTP = new SmtpClient(“yourexchangeserver”);
return true;
catch (Exception ex)
//Exception handling code
return false;


This site one of the best on ASP.NET tutorials has a tutorial that covers mass email from database


I hope this help


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: