接收-SMTP实例没有属性错误

发布于 2025-02-10 08:40:44 字数 1981 浏览 2 评论 0原文

我正在尝试使用Python脚本自动将电子邮件发送到客户列表。在他们制作之前,它可以很好地工作,因此您不能在2022年5月30日使用较不安全的应用程序。我添加了2FA密码并批准了我的计算机。我现在正在收到错误 server.send_message(消息) AttributeError:SMTP实例没有属性“ send_message” 尝试运行代码时。关于如何解决此问题的任何想法?我将附加下面的代码,当然还取出敏感信息。

Python代码

            MY_ADDRESS = "****@gmail.com"         # Email Address
            MY_PASSWORD = "****Password****"      # Emails 2FA Pass
            RECIPIENT_ADDRESS = ['****@gmail.com', '****@gmail.com']  # Recipient Address

            HOST_ADDRESS = 'smtp.gmail.com'   
            HOST_PORT = 587                         


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


            # Connection with the server
            server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
            server.starttls()
            server.login(MY_ADDRESS, MY_PASSWORD)

            # Creation of the MIMEMultipart Object
            message = MIMEMultipart()

            # Setup of MIMEMultipart Object Header
            message['From'] = MY_ADDRESS
            message['To'] = ", ".join(RECIPIENT_ADDRESS)
            message['Subject'] = "Test Email - July"

            # Creation of a MIMEText Part
            textPart = MIMEText("Hello **** & ****,\n\nAttached below is your test bill for the month of July 2022. \n\nBest,\Your Management", 'plain')

            # Creation of a MIMEApplication Part
            filename = "Test Bill - Billy.pdf"
            filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
            filePart["Content-Disposition"] = 'attachment; filename="%s' % filename

            # Parts attachment
            message.attach(textPart)
            message.attach(filePart)

            # Send Email and close connection
            server.send_message(message)
            server.quit()

I am trying to automate sending emails to a list of clients using a python script. It worked perfectly before they made it so you can't use the less secure app on 30th May 2022. I added the 2FA password and approved my computer. I am now receiving the error
server.send_message(message)
AttributeError: SMTP instance has no attribute 'send_message'
when I try to run my code. Any ideas on how to fix this? I will attach the code below and have of course taken out sensitive information.

Python Code

            MY_ADDRESS = "****@gmail.com"         # Email Address
            MY_PASSWORD = "****Password****"      # Emails 2FA Pass
            RECIPIENT_ADDRESS = ['****@gmail.com', '****@gmail.com']  # Recipient Address

            HOST_ADDRESS = 'smtp.gmail.com'   
            HOST_PORT = 587                         


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


            # Connection with the server
            server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
            server.starttls()
            server.login(MY_ADDRESS, MY_PASSWORD)

            # Creation of the MIMEMultipart Object
            message = MIMEMultipart()

            # Setup of MIMEMultipart Object Header
            message['From'] = MY_ADDRESS
            message['To'] = ", ".join(RECIPIENT_ADDRESS)
            message['Subject'] = "Test Email - July"

            # Creation of a MIMEText Part
            textPart = MIMEText("Hello **** & ****,\n\nAttached below is your test bill for the month of July 2022. \n\nBest,\Your Management", 'plain')

            # Creation of a MIMEApplication Part
            filename = "Test Bill - Billy.pdf"
            filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
            filePart["Content-Disposition"] = 'attachment; filename="%s' % filename

            # Parts attachment
            message.attach(textPart)
            message.attach(filePart)

            # Send Email and close connection
            server.send_message(message)
            server.quit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2025-02-17 08:40:44

尝试以下代码:

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


msg = MIMEMultipart()
body_part = MIMEText('Write you text here.')
user = '[email protected]'
password = 'xxxx'
msg['Subject'] = 'You subject'
msg['From'] = formataddr(('yyyyy', '[email protected]'))
msg['To'] = '[email protected]'
msg.attach(body_part)
smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", 465)
smtp_obj.login(user, password)
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
smtp_obj.quit()

更新:如何将文件附加到电子邮件:

只需将以下行添加到上面的脚本中即可。

from email.mime.application import MIMEApplication
path = './../' #The path of your file.

with open(path + filename,'rb') as file:    
        msg.attach(MIMEApplication(file.read(), Name='filename'))

Try the following code:

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


msg = MIMEMultipart()
body_part = MIMEText('Write you text here.')
user = '[email protected]'
password = 'xxxx'
msg['Subject'] = 'You subject'
msg['From'] = formataddr(('yyyyy', '[email protected]'))
msg['To'] = '[email protected]'
msg.attach(body_part)
smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", 465)
smtp_obj.login(user, password)
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
smtp_obj.quit()

Update: How to attach a file to your email:

Just add the following lines to the script above.

from email.mime.application import MIMEApplication
path = './../' #The path of your file.

with open(path + filename,'rb') as file:    
        msg.attach(MIMEApplication(file.read(), Name='filename'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文