Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday 18 March 2015

Some time it is needed to send multiple emails at a time in asp.net application. We can send email by using gmail credentials. In this article I have explained how to send email to multiple users in asp.net using gmail.
Source Code:
<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("Name", typeof(string));
        dt.Columns.Add("Email", typeof(string));
        dt.Rows.Add(1, "Ajay", "ajay@gmail.com");
        dt.Rows.Add(2, "Ankush", " ankush@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 = "This testing mail for multiple users";

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

            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) HandlesMe.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("Name", GetType(String))
        dt.Columns.Add("Email", GetType(String))
        dt.Rows.Add(1, "Ajay", "ajay@gmail.com")
        dt.Rows.Add(2, "Ankush", "ankush@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) HandlesbtnSend.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 = "This testing mail for multiple users"
            Dim sb As New StringBuilder()
            sb.Append("Hi  " + gridrow.Cells(1).Text.Trim())
            sb.Append(",<br/>")
            sb.Append("<br/>")
            sb.Append("<b>Welcome to Maziksolutions Blog</b><br/>")
            sb.Append("<br/>")
            sb.Append("<br/>")
            sb.Append("<b>Thanks</b>,<br> Mazik Solutions")
            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
Categories: , , ,

0 comments:

Post a Comment