Python: Get Gmail server with smtplib never ends
<p>I simply tried:</p>

<pre><code>&gt;&gt;&gt; import smtplib
&gt;&gt;&gt; server = smtplib.SMTP('smtp.gmail.com:587')
</code></pre>

<p>in my Python interpreter but the second statement never ends.</p>

<p>Can someone help? </p>

<p>You might find that you need a login and password as a prerequisite to a successful login-in.<br>
Try something like this:</p>

<pre><code>import smtplib
ServerConnect = False
try:
    smtp_server = smtplib.SMTP('smtp.gmail.com','587')
    smtp_server.login('your_login', 'password')
    ServerConnect = True
except SMTPHeloError as e:
    print "Server did not reply"
except SMTPAuthenticationError as e:
    print "Incorrect username/password combination"
except SMTPException as e:
    print "Authentication failed"
</code></pre>

<p>If you get "connection unexpected closed" try changing the server line to:</p>

<pre><code>smtp_server = smtplib.SMTP_SSL('smtp.gmail.com','465')
</code></pre>

<p>Be aware: Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe. See:<a href="https://support.google.com/accounts/answer/6010255?hl=en" rel="nofollow">https://support.google.com/accounts/answer/6010255?hl=en</a></p>

<p>Gmail settings:</p>

<pre><code>SMTP Server (Outgoing Messages)     smtp.gmail.com  SSL     465     
    smtp.gmail.com  StartTLS    587
IMAP Server (Incoming Messages)     imap.gmail.com  SSL     993    
    Please make sure, that IMAP access is enabled in the account settings.
Login to your account and enable IMAP.
You also need to enable "less secure apps" (third party apps) in the Gmail settings:
https://support.google.com/accounts/answer/6010255?hl=en
See also: How to enable IMAP/POP3/SMTP for Gmail account
</code></pre>

<p>If all else fails trying to <code>ping gmail.com</code> from the command line.    </p>

python smtplib