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:
 
SMTPServer: smtp.gmail.com  
SMTPPort: 465
SMTPUsername: ***@gmail.com  
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.To.Add(to);
            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());
            try
            {
                smtp.Send(mail);
            }
            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.
            }
            
            mail.Dispose();
        }

Open in new window


Help.
LVL 11
SweatCoderAsked:
Who is Participating?
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.

Frosty555Commented:
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:

https://www.google.com/accounts/UnlockCaptcha

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)
0

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.
0
Frosty555Commented:
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.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.