0

I am trying to send an email using SMTP client using my gmail credential. This is the code am using

        using (var mail = GetMailInfo())
        {
            using (SmtpClient client = new SmtpClient("smtp.gmail.com", 465))
            {
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    client.Credentials = new NetworkCredential("**@gmail.com", "Password");
                    client.UseDefaultCredentials = false;
                    client.EnableSsl = true;
                    client.Send(mail);
            }
        }

I receive a time out error

The operation has timed out

If I try again I get another error saying

Service not available, closing transmission channel. The server response was: Too many concurrent SMTP connections; please try again later.

What am I doing wrong?

N.J
  • 1,172
  • 3
  • 11
  • 19
  • You fail to read? The error message clearly states the connection was refused due to too many concurrent SMTP connections, and tells you to try later. WIth the information you provided that clearly points to the server being overloaded at the moment. – TomTom Sep 03 '20 at 09:09
  • @TomTom I indeed read that. But am sending only one email one time its a time out and another time instead of time out this one comes. – N.J Sep 03 '20 at 09:12
  • And? That is not YOUR server - the SERVER may be overloaded. THAT is what it tells you. If YOU do not cause it - well, it still is overloaded. You send like someone in a traffic jam saying "why is there a traffic jam, I am only here with one car".. And, btw., one mail at a time like this is abuse of the SMTP protocol. It is not meant to have a new connection per email. – TomTom Sep 03 '20 at 09:15
  • @TomTom Ok What would you suggest ? – N.J Sep 03 '20 at 09:22
  • Where did you get sample code? Your code is using port 465 which is SSL and you probably need to use TLS port 587. See : https://www.siteground.com/kb/google_free_smtp_server/ – jdweng Sep 03 '20 at 09:44
  • This can help https://stackoverflow.com/questions/9801224/smtpclient-with-gmail – Eugene Dianov Sep 03 '20 at 10:30

1 Answers1

0

At last I figure it out what was wrong with my code Updated code is below

using (var mail = GetMailInfo())
    {
        using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587)) // port is 587 instead of 465 as mentioned by @jdweng in the comment
        {
                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false; // this must be before the **Credentials** else this will reset the given credential                   
                client.Credentials = new NetworkCredential("**@gmail.com", "Password");
                
                client.EnableSsl = true;
                client.Send(mail);
        }
    }

On the top if you are using gmail go to settings and deactivate 2 way authentification and Access for less secure apps should be Activated

All these can be found Here

N.J
  • 1,172
  • 3
  • 11
  • 19