Categories
Asp, Asp.net

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.

Design:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send Mail From Database In Asp.Net</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<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>
</div> 
</form>
</body>
</html>

Code:

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");
        con.Open();
        string str = "select * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        ArrayList emailArray = new ArrayList();
        
        while (reader.Read())
        {
            emailArray.Add(reader["uemail"]);    
        }
        foreach (string email in emailArray)
        {
            const string username = "hightechnology@gmail.com";
            const string password = "hightech";
            SmtpClient smtpclient = new SmtpClient();
            MailMessage mail = new MailMessage();
            MailAddress fromaddress = new MailAddress("hightechnology@gmail.com");
            smtpclient.Host = "smtp.gmail.com";
            smtpclient.Port = 587;
            mail.From = fromaddress;
            mail.To.Add(email);
            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>"+
            "</body></html>";
            smtpclient.EnableSsl = true;
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
            smtpclient.Send(mail);
            lblMessage.Text = "Your email was sent";

        }
        reader.Close();
        con.Close();
    }
}

Comments are closed.