0

So, I am working on windows Forms and trying so send email with smtp. Here is a Code:

MailMessage mail = new MailMessage(from, to, subject, text);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 465;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(from, password);

try
{
    client.Send(mail);
    MessageBox.Show("Mesage has benn sant");
}
catch (Exception ex)
{
    MessageBox.Show("Failure while sending message");
    MessageBox.Show(ex.Message);
}

When I run this Code I am getting Following Error:

"Failure sending email".

When I changed port to 587 I have got following:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication REquired".

So why I cannot send email ? Can someone explain me that ?

I have changed port to 25 and have sent mail from hotmail( I have replaced "smtp.gmail.com" with "smtp.live.com"), not from gmail. And it works. Seems it's something wrong with gmail.

dave1993
  • 7
  • 7

3 Answers3

0

Firstly change the port to 25.

Also, the arguments for client.Credentials are supposed to be your username and password to login with. For example...

client.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "mypassword");
Bink
  • 91
  • 6
  • I have changed port to 25 but it gives me still this error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication REquired" – dave1993 Jan 31 '16 at 10:51
  • Did you change the credentials to a valid Gmail account? – Bink Jan 31 '16 at 10:55
  • On my gmail account I have got following message : " Hi Dave, Someone just tried to sign in to your Google Account Username@gmail.com from an app that doesn't meet modern security standard" – dave1993 Jan 31 '16 at 11:08
  • Hi, sorry for not responding quickly. [This thread](http://serverfault.com/questions/662568/why-blocks-gmail-my-postfix-relay) gives more insight and solutions about the situation. – Bink Jan 31 '16 at 12:12
0

I was able to make your code work by adding the following three lines and making change in the gmail account setting:

        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential (from, password);

In addition to the above changes, it turns out, you also need to update your google account settings and allow less secure apps.

I learned about this on this thread: Sending email in .NET through Gmail

Hope this helps.

Community
  • 1
  • 1
frostedcoder
  • 153
  • 7
-1

I had the same problem. Was able to fix it by disabling this security setting:

going to this link and turning on "access for less secure apps"

https://www.google.com/settings/security/lesssecureapps

enter image description here