首页 > 解决方案 > 邮箱帐户。Python 3.8 空闲脚本。错误:smtplib.SMTPSenderRefused: (503, b'5.5.1 EHLO/HELO first

问题描述

我正在做一个关于在 python 3.8 idle (Mac) 中编写模块以从我的 gmail 帐户发送电子邮件的练习。它给了我错误: smtplib.SMTPSenderRefused: (503, b'5.5.1 EHLO/HELO 首先。

完整的运行结果:

= RESTART: /Users/mimikatz/Desktop/python/Python_note&exercise/send_email_gmail.py

person_name
Thank you for sharing! 

Traceback (most recent call last):
File "/Users/mimikatz/Desktop/python/Python_note&exercise/send_email_gmail.py", line 58, in <module>
main()

File "/Users/mimikatz/Desktop/python/Python_note&exercise/send_email_gmail.py", line 51, in main
s.send_message(msg)

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 970, in send_message
return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 871, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (503, b'5.5.1 EHLO/HELO first. c18sm12642612wmk.18 - gsmtp', 'xxxxx@gmail.com')

前两行(person_name 谢谢分享!)来自“message.txt”文件。

我的代码是:

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

from_addr = 'xxxxxg@gmail.com'
password = 'xxxxxxxxxxx'
smtp_server = 'smtp.gmail.com'

def get_contacts(self):
    names = []
    emails = []
    with open(self, 'r', encoding='utf-8') as contacts_file:
        for a_contact in contacts_file:
            names.append(a_contact.split()[0])
            emails.append(a_contact.split()[1])
    return names, emails

def read_template(self):
    with open(self, 'r', encoding='utf-8') as template_file:
        template_file_content = template_file.read()
    return Template(template_file_content)

def main():
    names, emails = get_contacts('contacts.txt') # read contacts
    message_template = read_template('message.txt')

    # set up the SMTP server
    s = smtplib.SMTP_SSL(smtp_server)
    s.ehlo()
    s.connect(smtp_server,465)
    s.login(from_addr, password)

    # For each contact, send the email:
    for name, email in zip(names, emails):
        msg = MIMEMultipart()     # create a message
        # add in the actual person name to the message template
        message = message_template.substitute(person_name = name.title())
        # prints out the message body for our sake
        print(message)
        # setup the parameters of the message
        msg['From'] = from_addr
        msg['To'] = email
        msg['Subject'] = 'This is TEST'
        # add in the message body
        msg.attach(MIMEText(message, 'plain', 'utf-8'))
        # send the message via the server set up earlier.
        s.send_message(msg)
        del msg

    # Terminate the SMTP session and close the connection
    s.quit()

if __name__ == '__main__':
    main()

我在此处提出的一个问题中看到了此代码,作为示例对我来说看起来很棒,所以我想尝试一下。但我的问题与原来的问题不同。从运行结果看,“”“print(message)”“”命令加载成功。问题出现在 """s.send_message(msg)"""。我在网上查了几个类似的案例,但找不到适合这种情况的答案。

非常感谢任何帮助:)

菖蒲

感谢 Konrad 解决了问题

用 .connect() 交换 .ehlo()

& 将密码更改为通过 gmail 设置两步验证应用密码生成的验证码。我使用“mail”和“mac”来生成验证码。

标签: python-3.xgmail

解决方案


虽然我从未使用 Python 发送电子邮件,但我已经花了一些时间通过 Telnet 与 SMTP 服务器进行通信。

您需要先连接到服务器,然后发送 EHLO,然后进行身份验证,最后发送消息。您的代码似乎在连接到服务器之前尝试发送 EHLO。

尝试交换这两行。


推荐阅读