mail sending with network credential as true in windows form not working

I want to do an application for sending emails in C# windows application.I used smtp server,but I don't want to set the network credentials. So I set it as true.but am getting error.

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Here is the code:

SmtpClient oClient = new SmtpClient();
oClient.Host = "smtp.gmail.com";
oClient.Port = 25;
oClient.UseDefaultCredentials = true;
oClient.Credentials = new System.Net.NetworkCredential();
oClient.EnableSsl = true;
MailMessage oMail = new MailMessage();
oMail.To.Add(txtTo.Text.Trim());
oMail.From = new MailAddress("[email protected]");
oMail.Subject = txtSubject.Text;
oMail.Body = txtBody.Text;
oMail.IsBodyHtml = true;
oClient.Send(oMail);
MessageBox.Show("Mail Send");

here I set the host as gmail.com,I need to send and receive mails using all email service providers.So how can I set the host and port?


gmail uses port 587

oClient.Port = 587;

You may also want to set UseDefaultCredentials to false and explicitly declare username and password. By declaring it to be true, you are trying to log into your gmail account using your windows credentials.

oClient.UseDefaultCredentials = false;
oClient.Credentials = new NetworkCredential("[email protected]", "password");

Also, in gmail security, you will need to allow Less Secure Applications.

Finally, you need to change how your Mail.To is populated:

oMail.To.Add(new MailAddress(txtTo.Text.Trim()));

Google may block sign in attempts from some apps or devices when you try to login from some app. You need to go to security settings at your gmail and enable less secure apps . So that you will be able to login from all apps. Go to Allow less secure apps and choose Allow to let less secure apps access your Google account. Have a look at this link may useful.