Send Bulk Email in ASP.NET C# Sample Code

1 comment
In this Demo, We have to study how to Send Bulk Email in ASP.NET Website.

Default.aspx
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
    <table>
    <tr>
    <td>
        <asp:Label ID="lblTo" runat="server" Text="Label"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
        <asp:Label ID="lblSubject" runat="server" Text="Subject"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
        <asp:Label ID="lblMessage" runat="server" Text="Message"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtMessage" TextMode="MultiLine" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2">
        <asp:Button ID="btnSend" runat="server" Text="Button" onclick="btnSend_Click" />
    </td>
    </tr>
    <tr>
    <td colspan="2">
        <asp:Label ID="lblSent" runat="server"></asp:Label>
    </td>
    </tr>
    </table>
    </ContentTemplate>
     </asp:UpdatePanel>
    </div>
    </form>
You can add multiple email by separated with a comma in to Receiver TextBox.

Default.aspx.cs
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            string[] to = txtTo.Text.Split(';');
            foreach (string emailAdd in to)
            {
                if (!string.IsNullOrEmpty(emailAdd))
                    SendEmail(emailAdd);
            }
            txtTo.Text = "";
            txtSubject.Text = "";
            txtMessage.Text = "";
            lblSent.Text = "Message sent.";
        }
        catch
        {
            lblSent.Text = "Message failed.";
        }
    }
    private void SendEmail(string EmailAddress)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(EmailAddress);
        mail.From = new MailAddress("Your Email ID");
        mail.Subject = txtSubject.Text;
        mail.Body = txtMessage.Text;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential("Your Email ID", "Your Password");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}

Download Complete Source Code

1 comment:

  1. Send E-mail using ASP.NET, C#

    The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets to send or receive data over the Internet. SMTP protocol is using for sending email from C#. C# use System.Net.Mail namespace for sending email.
    To send e-mail you need to configure SMTP server. If you don’t have any SMTP, you can use free SMTP server. You can also use your gmail account.

    The following C# source code shows how to send an email using SMTP server.

    Code is here :
    http://cybarlab.blogspot.com/2013/03/send-e-mail-using-c-sharp.html

    ReplyDelete