Gmail account. Python 3.8 idle script. Error: smtplib.SMTPSenderRefused: (503, b'5.5.1 EHLO/HELO first

Gmail account. Python 3.8 idle script. Error: 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 解决了问题

将 .ehlo() 与 .connect() 交换

& 将密码更改为从 gmail 设置两步验证应用程序密码生成的验证码。我用“mail”&“mac”生成验证码。

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

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

尝试交换这两行。