Tuesday, March 17, 2015

Send email to multiple users in asp.net (C#, VB)

Introduction: In this article I will explain how to send email to multiple users using asp.net (C#, VB)

Description:

Html Markup Of page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="Gridview1" runat="server">
<HeaderStyle Font-Bold="true" />
</asp:GridView>
</div>
<asp:Button ID="btnSend" Text="Send Mail" runat="server" OnClick="btnSend_Click" />
    </div>
    </form>
</body>
</html> 


C# Code:
using System.Data;
using System.Net.Mail;
using System.Text;
  
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    protected void BindGrid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(Int32));
        dt.Columns.Add("Student Name", typeof(string));
        dt.Columns.Add("Email", typeof(string));
        dt.Rows.Add(1, "Vijay", "saklanivijay87@gmail.com");
        dt.Rows.Add(2, "Vijay Saklani", "saklanivijay@ymail.com");
        dt.Rows.Add(3, "John", "john@gmail.com");
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gridrow in Gridview1.Rows)
        {
            string studentname = gridrow.Cells[1].Text.Trim();
            string Email = gridrow.Cells[2].Text.Trim();
            string to = Email;
            MailMessage mail = new MailMessage("Sender Email", to);
            mail.Subject = "Test Email";

            StringBuilder sb = new StringBuilder();
            sb.Append("Hi  " + gridrow.Cells[1].Text.Trim());
            sb.Append(",<br/>");
            sb.Append("<br/>");
            sb.Append("<b>Welcome to Articlemirror</b><br/>");
            sb.Append("<br/>");
            sb.Append("<br/>");
            sb.Append("<b>Thanks</b>,<br> Articlemirror");

            mail.Body = sb.ToString();
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("Email Id", "Password");
            smtp.EnableSsl = true;
            mail.IsBodyHtml = true;
            smtp.Send(mail);
        }
    }

VB Code:
Imports System.Data
Imports System.Net.Mail
Imports System.Text

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub
    Protected Sub BindGrid()
        Dim dt As New DataTable()
        dt.Columns.Add("Id", GetType(Int32))
        dt.Columns.Add("Student Name", GetType(String))
        dt.Columns.Add("Email", GetType(String))
        dt.Rows.Add(1, "Vijay", "saklanivijay87@gmail.com")
        dt.Rows.Add(2, "Vijay Saklani", "saklanivijay@ymail.com")
        dt.Rows.Add(3, "John", "john@gmail.com")
        Gridview1.DataSource = dt
        Gridview1.DataBind()
    End Sub

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        For Each gridrow As GridViewRow In Gridview1.Rows
            Dim studentname As String = gridrow.Cells(1).Text.Trim()
            Dim Email As String = gridrow.Cells(2).Text.Trim()
            Dim [to] As String = Email
            Dim mail As New MailMessage("Sender Email", [to])
            mail.Subject = "Test Email"
            Dim sb As New StringBuilder()
            sb.Append("Hi  " + gridrow.Cells(1).Text.Trim())
            sb.Append(",<br/>")
            sb.Append("<br/>")
            sb.Append("<b>Welcome to Articlemirror</b><br/>")
            sb.Append("<br/>")
            sb.Append("<br/>")
            sb.Append("<b>Thanks</b>,<br> Articlemirror")
            mail.Body = sb.ToString()
            mail.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.Port = 587
            smtp.Credentials = New System.Net.NetworkCredential("Email Id", "Password ")
            smtp.EnableSsl = True
            mail.IsBodyHtml = True
            smtp.Send(mail)
        Next

    End Sub

Result:-
Send email

Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment