Cheap and Reliable Hosting :: How to Send Mail to Multiple Users Using Parallel Programming in ASP.NET

How to Send Mail to Multiple Users Using Parallel Programming in ASP.NET

IHostAzure.com| cheap and reliable ASP.NET 5 Hosting. Hi guys today I will sharing code about how to Send Mail to Multiple Users Using Parallel Programming in ASP.NET C#. Ok let me to show you.

The following is the table design from where I am fetching employee records.

Code

CREATE TABLE [dbo].[Employee](  
    [Emp_ID] [int] IDENTITY(1,1) NOT NULL,  
    [Name] [varchar](50) NULL,  
    [Email] [varchar](500) NULL,  
    [Designation] [varchar](50) NULL,  
    [City] [varchar](50) NULL,  
    [State] [varchar](50) NULL,  
    [Country] [varchar](50) NULL,  
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED   
(  
    [Emp_ID] ASC  
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  
) ON [PRIMARY]  
  
GO 

 The following is my aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SendMailToMultipleUsers.Default" %>  
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Send Mail To Multiple Users in ASP.NET C#</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
        <div>  
            <table style="border: solid 15px blue; width: 100%; vertical-align: central;">  
                <tr>  
                    <td style="padding-left: 50px; padding-top: 20px; padding-bottom: 20px;   
                            background-color: skyblue; font-size: 20pt; color: orangered;">  
                        Send Mail To Multiple Users in ASP.NET C#  
                    </td>  
                </tr>  
                <tr>  
                    <td style="text-align: left; padding-left: 50px; border: solid 1px red;">  
                        <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False" Width="90%"  
                            BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"  
                            CellPadding="4" GridLines="Both">  
                            <Columns>  
                                <asp:TemplateField HeaderText="Select">  
  
                                    <ItemTemplate>  
                                        <asp:CheckBox ID="chkSelect" runat="server" />  
                                    </ItemTemplate>  
                                </asp:TemplateField>  
                                <asp:BoundField DataField="Name" HeaderText="Employee Name" />  
                                <asp:BoundField DataField="Email" HeaderText="Email" />  
                                <asp:BoundField DataField="Designation" HeaderText="Designation" />  
                                <asp:BoundField DataField="City" HeaderText="City" />  
                                <asp:BoundField DataField="State" HeaderText="State" />  
                                <asp:BoundField DataField="Country" HeaderText="Country" />  
                            </Columns>  
                            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />  
                            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />  
                            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />  
                            <RowStyle BackColor="White" ForeColor="#003399" />  
                            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />  
                        </asp:GridView>  
                    </td>  
                </tr>  
                <tr>  
                    <td align="right">  
                        <asp:Button ID="btnSendMail" Text="Send Mail" OnClick="btnSendMail_Click" runat="server" />  
                    </td>  
                </tr>  
            </table>  
        </div>  
    </form>  
</body>  
</html> 
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Data;  
using System.Data.SqlClient;  
using System.Net.Mail;  
using System.Net;  
using System.Threading.Tasks;  
  
namespace SendMailToMultipleUsers  
{  
    public partial class Default : System.Web.UI.Page  
    {  
        SqlDataAdapter da;  
        DataSet ds = new DataSet();  
        DataTable dt = new DataTable();  
  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!Page.IsPostBack)  
                this.BindGrid();  
        }  
  
        private void BindGrid()  
        {  
            SqlConnection con = new SqlConnection();  
            ds = new DataSet();  
            con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";  
            SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);  
  
            da = new SqlDataAdapter(cmd);  
            da.Fill(ds);  
            con.Open();  
            cmd.ExecuteNonQuery();  
            con.Close();  
  
            if (ds.Tables[0].Rows.Count > 0)  
            {  
                GridViewEmployee.DataSource = ds.Tables[0];  
                GridViewEmployee.DataBind();  
            }  
        }  
  
        protected void btnSendMail_Click(object sender, EventArgs e)  
        {  
            DataTable dt = new DataTable();  
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name", typeof(string)),  
                        new DataColumn("Email",typeof(string)) });  
  
  
            foreach (GridViewRow row in GridViewEmployee.Rows)  
            {  
                if ((row.FindControl("chkSelect") as CheckBox).Checked)  
                {  
                    dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text);  
                }  
            }  
  
  
            string body = "Hi This is test Mail.<br /><br />Thanks.";  
  
  
            Parallel.ForEach(dt.AsEnumerable(), row =>  
            {  
                SendEmail(row["Email"].ToString(), row["Name"].ToString(), body);  
            });  
        }  
  
        private bool SendEmail(string To, string ToName, string body)  
        {  
            try  
            {  
                MailMessage mm = new MailMessage("yourEmailID@gmail.com", To);  
                mm.Subject = "Welcome " + ToName;  
                mm.Body = body;  
                mm.IsBodyHtml = true;  
                SmtpClient smtp = new SmtpClient();  
                smtp.Host = "smtp.gmail.com";  
                smtp.EnableSsl = true;  
                NetworkCredential NetworkCred = new NetworkCredential();  
                NetworkCred.UserName = "yourEmailID@gmail.com";  
                NetworkCred.Password = "<YourGmailPassword>";  
                smtp.UseDefaultCredentials = true;  
                smtp.Credentials = NetworkCred;  
                smtp.Port = 587;  
                smtp.Send(mm);  
                return true;  
            }  
            catch (Exception ex)  
            {  
                return false;  
            }  
        }  
    }  
}

 Happy Coding

Cheap and Reliable ASP.NET 5 Hosting 2015

ASPHostPortal.com

ASPHostPortal.com is the leading provider of Windows hosting and affordableASP.NET 5 Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.

HostForLIFE.eu

HostForLIFE.eu guarantees 99.9% uptime for their professional ASP.NET 5 hosting and actually implements the guarantee in practice. HostForLIFE.eu is the service are excellent and the features of the web hosting plan are even greater than many hosting. HostForLIFE.eu offer IT professionals more advanced features and the latest technology. HostForLIFE.eu has supported  ASP.NET 5,  Relibility, Stability and Performance of  servers remain and TOP priority. Even basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools.

DiscountService.biz

DiscountService.biz is The Best and Cheap ASP.NET 5 Hosting. DiscountService.biz was established to cater to an under served market in the hosting industry web hosting for customers who want excellent service. DiscountService.biz guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability. DiscountService.biz has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch. DiscountService.biz is devoted to offering the best Windows hosting solution for you.

Posted in Hosting Tutorial and tagged , , , , , .