Since your code looks good and the problem is with authentication. I think your password and/or email address are not valid or correct combination. If not that, then I guess you created a new account on Gmail and that is why, Google is not currently allow you to use API and SMTP calls to use the account. Google does that;
I also had this trouble with a new account that I created.
Also, remember to always call Dispose function on your objects that can be released. A good way to do that is using
using
block. So, just as a template, your code would look like this:
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
client.EnableSsl = true;
client.Credentials = new NetworkCredential("<username>", "<password>");
MailMessage message = new MailMessage(
"from@example.com",
"to@example.com",
"Hello",
"World!"
);
client.Send(message);
Console.WriteLine("Email has been sent.");
Console.Read();
}
What I use is:
1. Pass the values in the constructor; why waste another statement just to fill the values that can be filled in the constructor.
2. Use 25, which is the default SMTP port on TCP.
3. Do not tell the SMTP client to
not use the default credentials. It will know if you pass the network credentials object.
Always make sure the username and password combination is correct and that your account is configured to be used with SMTP protocols. You can check those settings in the Settings tab of Gmail. To ensure everything, try your actual account and not testing account.
For more, please read my article, it shares much about SMTP protocol support in .NET and how to tackle a few problems:
Sending emails over .NET framework, and general problems – using C# code[
^]