0

I would like to know why this code does not work. I'm on VSCode, python 3.8.0-32bit. I do not know if this problem comes from the code or from an action that I should have done on my gmail. Can you enlighten me on this please


sender_email = input(str("enter your email address: "))
password = input(str("enter your password: "))
rec_email = input(str("enter the email address to whom you want to send the message: "))
message = str("message envoyé avec python")
print(".")
server = smtplib.SMTP('smtp.gmail.com')
print(".")
server.starttls()
print(".")
server.login(sender_email,password)
print("login success")
server.sendmail(sender_email,rec_email,message)
print("email has be sent to", rec_email)```
And python sends me back:
```Windows PowerShell
Copyright (C) Microsoft Corporation. Tous droits réservés.

Testez le nouveau système multiplateforme PowerShell https://aka.ms/pscore6

PS C:\Users\*************> & C:/Users/evain/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/evain/OneDrive/Bureau/mail.py
enter your email address: ev******************4@gmail.com
enter your password: **********************
enter the email address to whom you want to send the message: ma*****************1@gmail.com
.
Traceback (most recent call last):
  File "c:/Users/***********/OneDrive/Bureau/mail.py", line 8, in <module>
    server = smtplib.SMTP('smtp.gmail.com')
  File "C:\Users\***********\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 256, in __init__
    raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (421, b'Cannot connect to SMTP server 2a00:1450:400c:c09::6c (2a00:1450:400c:c09::6c:25), connect error 10060')```
  • Does this answer your question? [Failing to send email with the Python example](https://stackoverflow.com/questions/399129/failing-to-send-email-with-the-python-example) – Mario Jul 03 '20 at 17:06
  • Your PC almost certainly does not have permission to connect anywhere on port 25 except to your own ISP's mail server (and even then you will probably need to authenticate). The spammers have been pissing in the pool for 25 years; you don't want to swim there. – tripleee Jul 30 '20 at 15:57
  • Also, the message you send needs to be a properly constructed SMTP message, not some random string. – tripleee Jul 30 '20 at 15:59

1 Answers1

0

My Outlook settings show that server “smtp.gmail.com” uses port 465 and SSL/TLS encryption. Therefore I use: server = smtplib.SMTP_SSL(‘smtp.gmail.com’) instead of: server = smtplib.SMTP(‘smtp.gmail.com’) server.starttls(). Being a novice on this subject, I found this solution under How to send an email with Gmail as provider using Python? .

Hans
  • 1