Thursday, 22 August 2013

How to send mail using gmail authentication in Asp.Net



using System.Net;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Data.OleDb;




    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress("brijeshmalasi@yahoo.com", "Gedutech");
            mailMsg.To.Add( new MailAddress("brjmalasi@gmail.com","global"));
         
            mailMsg.Subject = "hello";
            mailMsg.IsBodyHtml = true;
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.Body = "this is test mail";
            mailMsg.Priority = MailPriority.Normal;
            mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.Credentials = new NetworkCredential("your gmail id", "password");
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //you can also call client.Send(msg)
            client.Send(mailMsg);
           response.write("Sent");
        }
        catch (SmtpException ee)
        {
            Response.Write(ee);
        }


    }


0 comments:

Post a Comment