EDUDOTNET

Tuesday, May 31, 2016

Email Sending Using Gmail Account In C#

Email Sending Using Gmail Account In C#

- Following gmail smtp address is:-
- smtp.gmail.com and port:- 25, 587 - TLS
- same server and port:- 465 - SSL

- Log on using your gmail account use for sending mail and go to "Less secure apps" and turn on it. url: https://www.google.com/settings/security/lesssecureapps

- You need to enable allowing less secure apps otherwise you will get an authentication error.

- more details:- How to enable allowing less secure apps - you can follow steps mention here: https://support.google.com/accounts/answer/6010255?hl=en-GB



Below mention code for an example:

   MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("********.**@gmail.com");
            mail.To.Add("****.xyz@gmail.com");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("********.**@gmail.com", "********");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);