Alankar bhosale

Wednesday, May 13, 2015

Send email using gmail in .net

using System.Net.Mail;
using System.Net;

private void SendMail()
        {
           string sFromAddress = "YourMailId@gmail.com";
           string sToAddress = "ReceiversMailId@xyz.com";
           const string sFromPassword = "YourPassword";
           const string sSubject = "Mail subject";
           const string sBody = "Mail Body";


                SmtpClient smtp = new SmtpClient

               {

                   Host = "smtp.gmail.com", // smtp server address here…

                   Port = 587,

                   EnableSsl = true,

                   DeliveryMethod = SmtpDeliveryMethod.Network,

                   Credentials = new System.Net.NetworkCredential(sFromAddress,            sFromPassword),

                   Timeout = 30000,

               };

                MailMessage mmMessage = new MailMessage(sFromPassword, sToAddress, sSubject, sBody);

                smtp.Send(mmMessage);

}


 

if your mail account is not configured as less secure apps then it might possible that you will get smtpException. 

to avoid it go to the below link 

and configure gmail as shown in below image