SMTP gmail GeneralFailure - The operation has timed out

4 hours ago 1
ARTICLE AD BOX

I have the following SMTP code. When I run it in a console app, it works fine. After deploying it to the server and trying to run it as Windows service, I keep getting this error:

GeneralFailure - The operation has timed out

on this line of code:

smtpClient.Send(message);

I checked if the server has connection to smtp.gmail.com with port 587 and it does have that connection. Also, I've tried running the service using local system and Network Service, but I'm still getting the same error.

I don't why it keeps happening, but I feel it's related to the account running the service.

using (SmtpClient smtpClient = new SmtpClient()) { smtpClient.Host = ConfigurationManager.AppSettings["SmtpHost"];//smtp.gmail.com smtpClient.Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);//587 smtpClient.EnableSsl = bool.Parse(ConfigurationManager.AppSettings["SmtpEnableSsl"]);//true smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential( ConfigurationManager.AppSettings["SmtpUsername"], ConfigurationManager.AppSettings["SmtpPassword"] ); smtpClient.Timeout = 30000; string fromEmail = ConfigurationManager.AppSettings["EmailFromAddress"]; MailMessage message = new MailMessage { From = new MailAddress(fromEmail), Subject = subject, Body = emailBody, IsBodyHtml = true, BodyEncoding = Encoding.UTF8, SubjectEncoding = Encoding.UTF8 }; message.To.Add(recipientEmail); smtpClient.Send(message); }
Read Entire Article