简体   繁体   中英

How to send an email in python using smtplib

My code:

import smtplib

try:
    mail = smtplib.SMTP('smtp.gmail.com', 587)
    print("Connection successful")
except:
    print("Connection failed")

content = str(input("Your message: "))

senderLogin = str(input('Your full email (currently only GMail): '))
senderPass = str(input('Your password(data is not stored): '))

receiverAcc = str(input('Who would you like to send it to: '))

mail.ehlo()
mail.starttls()
mail.ehlo

mail.login(senderLogin, senderPass)
try:
    mail.sendmail(senderLogin, receiverAcc, content)
    print ("Email sent")

except:
    print('error')

mail.close()

I cannot get it to work. All that happens is that the shell pops up and it is a blank screen. Nothing else. If anyone can clarify and/or help, that would be great. Also, I have already enabled the lower-security apps in my google account.

UPDATE

I played around with the ports and found out that port 25 for smtp.gmail.com works.

It is possible you are being blocked by Google's SMTP server. You should try a different SMTP server and adjust usage of mail.starttls() as needed. Otherwise, your code should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM