所以我试图用下面的代码发送带有python的基本电子邮件,但它给我一个错误,我使用浏览器登录。我做到了,它仍然吐出同样的错误。 代码:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("projtig6@gmail.com", "redacted")
msg = "Your security system has detected an intrusion of some kind please refere to http://projetotig6.orgfree.com/ for an image of the current situation"
server.sendmail("projtig6@gmail.com", "grenskul@gmail.com", msg)
server.quit()
当我运行它时,它给了我这个
user@ubuntu:~/Desktop$ python3 tent.py
Traceback (most recent call last):
File "tent.py", line 5, in <module>
server.login("projtig6@gmail.com", "redacted")
File "/usr/lib/python3.4/smtplib.py", line 639, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtT\n5.7.14 NJKUGchcZYhLMqcl9utRyvWa6jwkzOCuhBtdP3URW63HNIVrSlue8To8yKxxbDIwvlnO2g\n5.7.14 V2GooN21jsn0X8_d6W_CxGwuOXmBrkoDzFCqqbB72xz3MbY0Kj3Z7ZzdXlQCc8sjdavwes\n5.7.14 bUlIhA8GIqRKJAyBbildXtsSqF8Fh_7AYPI0W3SKlidhoUOK7o4hI0IIUmgVdqOCpFpxyU\n5.7.14 fvShqxoTn6W_bAWqXfS5akND40-gQ> 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 c142sm10923092wme.18 - gsmtp')
答案 0 :(得分:0)
首先,请确保您允许安全性较低的应用对您的Gmail进行身份验证&#34;:https://support.google.com/accounts/answer/6010255?hl=en
如果这不是问题,请尝试使用我在python中发送电子邮件时写的包装器;它使发送电子邮件非常简单:https://github.com/krystofl/krystof-utils/blob/master/python/krystof_email_wrapper.py
像这样使用它:
emailer = Krystof_email_wrapper()
email_body = 'Hello, <br /><br />' \
'This is the body of the email.'
emailer.create_email('sender@example.com', 'Sender Name',
['recipient@example.com'],
email_body,
'Email subject!',
attachments = ['filename.txt'])
cred = { 'email': 'sender@example.com', 'password': 'VERYSECURE' }
emailer.send_email(login_dict = cred, force_send = True)
您还可以查看源代码以了解其工作原理。 相关位:
import email
import smtplib # sending of the emails
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
self.msg = MIMEMultipart()
self.msg.preamble = "This is a multi-part message in MIME format."
# set the sender
author = email.utils.formataddr((from_name, from_email))
self.msg['From'] = author
# set recipients (from list of email address strings)
self.msg['To' ] = ', '.join(to_emails)
self.msg['Cc' ] = ', '.join(cc_emails)
self.msg['Bcc'] = ', '.join(bcc_emails)
# set the subject
self.msg['Subject'] = subject
# set the body
msg_text = MIMEText(body.encode('utf-8'), 'html', _charset='utf-8')
self.msg.attach(msg_text)
# send the email
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(login_dict['email'], login_dict['password'])
session.send_message(self.msg)
session.quit()