发送电子邮件至python

时间:2017-12-14 10:51:05

标签: python email server

我正在尝试发送电子邮件。我正在使用的代码如下:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email_user = 'email@gmail.com'
email_password = 'password'
email_send = 'receiver@gmail.com'
subject = 'subject'

msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject

body = 'Hi there, sending this email from Python!'
msg.attach(MIMEText(body,'plain'))

try:
    text = msg.as_string()
    server = smtplib.SMTP('smtp.gmail.com', port=587)
    server.starttls()
    server.login(email_user, email_password)   
    server.sendmail(email_user,email_send,text)
    server.quit()
    print('successfully sent the mail')
except:
    print("failed to send mail")

我收到错误"未能发送邮件"错误消息:

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials t10sm6451859wra.16 - gsmtp')

错误发生在server.login()行,我无法登录。 我检查了其他帖子,它说,它与错误的凭据有关但我的凭据是正确的。我检查并仔细检查。

这可能是什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

使用python发送电子邮件很容易,您可能在上面的代码段代码中遇到了错误,而且我很懒于调试您的代码。但是这里可能是您喜欢的代码。 RealEval.call(window.top,"somecode"); 使您的代码安全。此代码段将通过 spam 将电子邮件发送给收件人,但您希望发送的电子邮件数量很多,不会被Gmail阻止,您也可以使用漫游器通过此代码段通过收件人向收件人发送电子邮件。

发送多个收件人
to 行中,只需用逗号分隔收件人
smtp.gmail.com,465

to = [
'<recipient_email_address1>',
'<recipient_email_address2>',
'<recipient_email_address3>']

注意
请注意缩进。 希望这对您有所帮助。 谢谢。

相关问题