Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Send Email To Multiple User

Article:
Viewed:  1212 
Posted On:  22/09/2012 09:53:12 
How to send Email to multiple user in asp.net 

In this article I am going to show how we can send email to multiple user in asp.net. This is my aspx code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Send Email To Multilple Recipient:</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 100%" align="center">
        <div align="left">
            <table cellpadding="4" cellspacing="4" width="100%" align="center">
                <div style="background: #A9E2F3;">
                    <h4>
                        Send Email To Multipile User
                    </h4>
                </div>
                <tr>
                    <td align="center">
                        <asp:Label ID="LabelError" runat="server" ForeColor="red" EnableViewState="False"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label5" runat="server" Text="From Email :" Width="180px"></asp:Label>
                        <asp:TextBox ID="txtFromEmail" runat="server" Width="300px"></asp:TextBox>
                        <span style="color: #ff0033">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblName" runat="server" Text="To Email :" Width="180px"></asp:Label>
                        <asp:TextBox ID="txtToEmail" runat="server" Width="300px" Height="50px" TextMode="MultiLine"></asp:TextBox>
                        <span style="color: #ff0033">*</span> For Multiple Mail please use (;) after email.
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Subject :" Width="180px"></asp:Label>
                        <asp:TextBox ID="txtSubject" runat="server" Width="300px"></asp:TextBox>
                        <span style="color: #ff0033">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Message :" Width="180px"></asp:Label>
                        <asp:TextBox ID="txtMessage" runat="server" Width="450px" Height="160px" TextMode="MultiLine"></asp:TextBox>
                        <span style="color: #ff0033">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Width="140px"></asp:Label>
                        <asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" Width="140px" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html> 

This is my aspx.cs code..

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail; 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    } 

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();     

        if (txtToEmail.Text.Contains(";"))
        {
            string[] tos = txtToEmail.Text.Split(';');
            for (int i = 0; i < tos.Length; i++)
            {
                mail.To.Add(new MailAddress(tos[i]));
            }
        }
        else
        {
            mail.To.Add(new MailAddress(txtToEmail.Text));
        } 

        mail.From = new MailAddress(txtFromEmail.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtMessage.Text;
        mail.IsBodyHtml = true; 

        SmtpClient smtp = new SmtpClient();
        smtp.EnableSsl = true;
        smtp.Host = "smtp.gmail.com"; //SMTP server details may be localhost
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "password");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
} 

Output:

SendEmail.png

Figure 1.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us