Thursday, 22 October 2015

Sending Mail through programming using Gmail Account

Use the below coding in your c# web application.
public void SendMailTo()
        {
            try
            {
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.Port = Convert.ToInt16(587);

                client.EnableSsl = true;
                client.Timeout = 100000;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential("YourMailId@gmail.com", "Your Password");

                MailMessage msg = new MailMessage();
                msg.IsBodyHtml = true;
                msg.To.Add(ToAddress);

                msg.CC.Add(CCAddress); 
                msg.Bcc.Add(BccAddress);
              
                msg.From = new MailAddress("YourMailId@gmail.com");
                msg.Subject = "Subject";

                string sBody = "Dear Sir/Mam";
                sBody += "<br/><br/>Your Message";

                msg.Body = sBody;
                client.Send(msg);
            }
            catch (Exception ex)
            {
               
            }
        }

Settings in Gmail Account:
Enable your Pop settings.


No comments: