有一段时间没有使用过python,但是需要一个脚本给自己发送警报等信息。
为什么不这样做?在gmail告诉我一个不安全的应用程序尝试访问后,我切换到使用ssl。
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
msg = MIMEText("TEST FILE")
me = 'me@gmail.com'
you = me
msg['Subject'] = 'This is a test'
msg['From'] = me
msg['To'] = you
#username and password here
username = 'username'
password = 'password'
# Send the message via gmail with ssl
s = smtplib.SMTP_SSL('smtp.gmail.com:465')
s.login(username,password)
s.send_message(msg)
s.quit()
我尝试运行时遇到的错误如下:
Traceback (most recent call last):
File "F:\Desktop\emailTest.py", line 24, in <module>
s.login(username,password)
File "F:\Python34\lib\smtplib.py", line 652, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsJy\n5.7.14 DZytmH_1-mwcm6vOSQ8erg0EXRR-OzQ6-shmU4noqJdeRPdzO62-ZIC2GPMtLNj3u5-qxJ\n5.7.14 ZT3QrgTcO7d5zpJ8UtO2vvTFU1G2rmMWfBwpvavpD8__GaVCWHWszps1GzZqxnxJbqD1DA\n5.7.14 EX-9IS6nxe9-giAhaFj5FryML1r0Qr1wcUH9tEmRdjX4KlO1xX4BhPBMJ5GJ7imrzrLZUv\n5.7.14 NgG0BaLrwWzUK1SSd_bqptGe15rg> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 uo9sm14972088wjc.49 - gsmtp')