Wednesday 1 April 2015

Send eMail by using Gridview data (ASP.NET AND C#)

Here I am explaining about how to send eMail of Gridview data via ASP.NET and C#

C# CODE

CLASS LIBRARY

using System.Net;

using System.Net.Mail;

 using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                gvcsbytime.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                MailMessage mm = new MailMessage("from@mail.in", "to@mail.in");
                mm.Subject = "Calls Status as on " + DateTime.Now.Date.Day + "-" + DateTime.Now.Date.Month + "-" + DateTime.Now.Date.Year + " " + DateTime.Now.TimeOfDay.Hours + "-" + DateTime.Now.TimeOfDay.Minutes + "-" + DateTime.Now.TimeOfDay.Seconds;
                mm.Body = "Dear " + Session["Query"].ToString() + ", <br> <br> Please find below the Calls status as on " + DateTime.Now.Date.Day + "-" + DateTime.Now.Date.Month + "-" + DateTime.Now.Date.Year + " " + DateTime.Now.TimeOfDay.Hours + "-" + DateTime.Now.TimeOfDay.Minutes + "-" + DateTime.Now.TimeOfDay.Seconds + "<hr/>" + sw.ToString() + "<br> <br> This is a self generated mail. Please do not reply on this ID <br> <br> MIS";
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.bizmail.yahoo.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "from@mail.in";
                NetworkCred.Password = "password";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            }

        }

Hope this will help you, if you have any query please revert.