- Sender Email Address
- Sender Password
- Receiver Email Address
- Port Number
- EnableSSL property
And most important before we start coding we need to import two namespaces to access the MailMessage Class:
- using System.Net;
- using System.Net.Mail;
Note
EnableSSL property generally specifies whether SSL is used to access the specified SMTP mail server or not. Please go through the piece of code to send an email as given below. Nowadays Gmail has become more secure due to various security reasons, so while sending email using mail message class we get the Authentication Error also. Please follow the steps given below to change some settings in Sender Gmail account to get rid of that error.
- using System.Net;
- using System.Net.Mail;
- namespace SendMail {
- class Program {
- static string smtpAddress = "smtp.gmail.com";
- static int portNumber = 587;
- static bool enableSSL = true;
- static string emailFromAddress = "[email protected]"; //Sender Email Address
- static string password = "Abc@123$%^"; //Sender Password
- static string emailToAddress = "[email protected]"; //Receiver Email Address
- static string subject = "Hello";
- static string body = "Hello, This is Email sending test using gmail.";
- static void Main(string[] args) {
- SendEmail();
- }
- public static void SendEmail() {
- using(MailMessage mail = new MailMessage()) {
- mail.From = new MailAddress(emailFromAddress);
- mail.To.Add(emailToAddress);
- mail.Subject = subject;
- mail.Body = body;
- mail.IsBodyHtml = true;
- //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment
- using(SmtpClient smtp = new SmtpClient(smtpAddress, portNumber)) {
- smtp.Credentials = new NetworkCredential(emailFromAddress, password);
- smtp.EnableSsl = enableSSL;
- smtp.Send(mail);
- }
- }
- }
- }
- }

Follow the below steps to get rid of this error.
- Sign in to your Gmail account and go to settings.
- Go to Account and Import and click on other Google Account settings.

- Then click on

- Turn this ON (Allow less secure app)

puneet mishraPosted Sep 26, 2022, 12:13 PM
Google stopped the less secure app permission , now how to send mail through your code please let me know
puneet mishraPosted Sep 26, 2022, 12:12 PM
How to use this code after 30 may . google stop the Less secure app setteing
achal prajapatiPosted Jun 22, 2022, 2:47 PM
I am not able to see this option less secure and still facing same issue.
Paulo MacielPosted Mar 23, 2022, 3:18 PM
Thanks for the code, man! It helped a lot.
Guest UserPosted Aug 11, 2020, 11:39 AM
Did the solution told above, the code ran for me. Thanks
KALYANA KUMAR PPosted Apr 27, 2020, 2:47 AM
Thanks for the code. Even if I tried disabling the google setting still I'm facing the same issue.
Subhash TiwaryPosted Feb 16, 2020, 8:26 AM
Thanks for sharing this article. I am able to send email using this. But when I send email to multiple users it is not delivered to them. Single email is ok and working but bulk email is not working. I can see the mail in the sent mail option but the mails are not delivering.
jubula samalPosted May 28, 2019, 9:14 AM
Nice article............
Bhavesh JadavPosted Jan 17, 2019, 6:59 AM
Nice........
J BPosted Jan 16, 2019, 10:31 AM
Hi, two questions: 1) can this be adapted to run in a classic Microsoft Access application? 2) can one auto attach a PDF file using a defined path/file name? thanks...
MrnamsPosted Nov 19, 2018, 11:04 AM
It's not showing Sender Email Address.
Deepak SinghPosted Oct 5, 2018, 7:25 AM
Check your gmail account has dual authentication, remove from your account, it will work.
Nithish ReddyPosted Sep 21, 2018, 2:11 AM
I HAVE DONE THE ABOVE STEPS TOO !
Nithish ReddyPosted Sep 21, 2018, 2:11 AM
Error : SEVER DOES NOT SUPPORT SECURE CONNECTIONS !
chinni arvindPosted Sep 19, 2018, 5:47 AM
Im getting an {"Authentication failed because the remote party has closed the transport stream."} error , did Anyone run into the same problem??
pradeepkumar yadavPosted Sep 8, 2018, 5:56 AM
Nice thanks its helpful to me
Rajesh KumarPosted Jul 19, 2018, 1:48 AM
Nice article...........
Bhavesh JadavPosted Apr 1, 2018, 11:47 PM
Good...........................................