can't send email via gmail

I'm using a custom ASP.NET app that occasionally needs to send an email to a user. No, it's not spam. My app has been gathering dust for months. Previously I could always send emails from my app using my gmail account and smtp configs. But I tried today and it no longer works. I triple checked credentials and settings, and I just can't figure out what the problem is.
Here are my smtp settings:
SMTPPort: 465
SMTPUsername: ***  
SMTPPassword: ****  
SMTPSSLEnabled: true
The usn (email) and password are correct, I just obfuscated them for this post.
Error message: Failure sending mail.
Here's the c# code I'm using:

 public static void SendEmail(string from, string to, string subject, string body, bool isHTML)
            var mail = new MailMessage();
            if (from == string.Empty)
                from = ConfigurationManager.AppSettings["SystemEmailFrom"];
            mail.From = new System.Net.Mail.MailAddress(from);
            // block below can optionally override the email recipient with an email address from Web.config
            if (ConfigurationManager.AppSettings["EmailOverride"] != string.Empty)
                to = ConfigurationManager.AppSettings["EmailOverride"];
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isHTML;
            SmtpClient smtp;
            smtp = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"], int.Parse(ConfigurationManager.AppSettings["SMTPPort"]));
            smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTPUsername"], ConfigurationManager.AppSettings["SMTPPassword"]);
            smtp.EnableSsl = bool.Parse(ConfigurationManager.AppSettings["SMTPSSLEnabled"]);
            smtp.Timeout = 4000; // timeout after 4 seconds so the user doesn't have to sit and wait when something goes wrong with email
            //smtp.SendCompleted += new SendCompletedEventHandler(SendEmailCompletedCallback);
            //smtp.SendAsync(mail, GetUserIDFromCookie());
            catch (Exception ex)
                string errorMessage = ex.Message;
                if (errorMessage.Length > 3500)
                    errorMessage = errorMessage.Substring(0, 3500); // Truncate if necessary; must leave room for credentials at the end.
                string creds = "SMTPServer: " + ConfigurationManager.AppSettings["SMTPServer"] + Environment.NewLine;
                creds += "SMTPPort: " + ConfigurationManager.AppSettings["SMTPPort"] + Environment.NewLine;
                creds += "SMTPUsername: " + ConfigurationManager.AppSettings["SMTPUsername"] + Environment.NewLine;
                creds += "SMTPPassword: " + ConfigurationManager.AppSettings["SMTPPassword"] + Environment.NewLine;
                creds += "SMTPSSLEnabled: " + ConfigurationManager.AppSettings["SMTPSSLEnabled"] + Environment.NewLine;
                WriteLog("SendEmail()", errorMessage + Environment.NewLine + creds);
                // SWALLOW this exception (after logging); do not raise back to the user.

Open in new window

LVL 11
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gmail has tightened their security for SMTP in the last little while. You may need to unlock your account to allow it to be used for IMAP/SMTP by using google's captcha unlock available here:

Otherwise, you can also try to use non-SSL encrypted connection on port 587 (but make sure you use STARTTLS security) for your smtp connection.

Sometimes I've had issues with google's SSL certificates not being trusted. Make sure your program isn't failing because of an untrusted / incorrect certificate (make sure your program ignores certificate validation, the problem is on Google's end, not yours)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SweatCoderAuthor Commented:
When I changed the smtp port to 587 everything worked. Apparently .net can't send smtp through gmail via port 465. I still had to leave smtp ssl enabled for this to work, though, which surprised me.
Check your antivirus as well, some antivirus will expect you to configure your mail clients using unencrypted IMAP/SMTP connection, even though the server is expecting an encrypted connection. The Antivirus checks the messages for viruses and then applies its own layer of encryption before sending it to the mailserver. I know Avast Antivirus works that way, at least.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.