How To Send Multiple Emails From Database In ASP.NET Using C#

How To Send Multiple Emails From Database In ASP.NET Using C##

In this tutorial we will explain How To Send Multiple Emails From Database In ASP.NET Using C#.Here we have stored email list in SQL Server database.Here we will send mails to whole list one by one by using foreach loop.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>Send Mail From Database In Asp.Net</title>
<style type="text/css">
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
<form id="form1" runat="server">
<h2>Send Mail From Database In Asp.Net</h2>
<div style="width:250px;margin:0px auto">

<br /><br /><br />
Subject: <asp:TextBox ID="txtsub" runat="server" Width="250px" Height="35px"></asp:TextBox><br /><br />
Message: <asp:TextBox ID="txtmsg" runat="server" Width="250px" Height="50px" TextMode="MultiLine"></asp:TextBox><br />
<asp:Button ID="send" Text="Send Mails" runat="server" OnClick="send_Click" />

<br /><br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>


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

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void send_Click(object sender, EventArgs e)
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dev;User ID=sa;Password=hightech");
        string str = "select * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        ArrayList emailArray = new ArrayList();
        while (reader.Read())
        foreach (string email in emailArray)
            const string username = "";
            const string password = "hightech";
            SmtpClient smtpclient = new SmtpClient();
            MailMessage mail = new MailMessage();
            MailAddress fromaddress = new MailAddress("");
            smtpclient.Host = "";
            smtpclient.Port = 587;
            mail.From = fromaddress;
            mail.Subject = (txtsub.Text);
            mail.IsBodyHtml = true;
            mail.Body ="<html><head><title>" + HttpUtility.HtmlEncode(txtsub.Text) + "</title></head><body  style='height:600px'>" +
            "<p>Message: " + HttpUtility.HtmlEncode(txtmsg.Text) + "</p>"+
            smtpclient.EnableSsl = true;
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
            lblMessage.Text = "Your email was sent";