Love__Tay 2025-06-07 17:41 采纳率: 64.7%
浏览 11

Python实现发送邮件的功能报错

在实现发送邮件的功能时,我的代码出现报错:

img

我的实现:

def send_email(self):
        # build email message
        msg = EmailMessage()
        msg['Subject'] = f'Daily Digest - {datetime.date.today().strftime("%d %b %Y")}'
        msg['From'] = self.sender_credentials['email']
        msg['To'] = ', '.join(self.recipients_list)

        # add Plaintext and HTML content
        msg_body = self.format_message()
        msg.set_content(msg_body['text'])
        msg.add_alternative(msg_body['html'], subtype='html')

        # secure connection with STMP server and send email
        with smtplib.SMTP('smtp-mail.outlook.com', 587) as server:
            server.starttls()
            server.login(self.sender_credentials['email'],
                         self.sender_credentials['password'])
            server.send_message(msg)

我已经根据https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040 打开了权限,还是出现上图报错。

img


请问该怎么解决🙂

  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-06-07 17:41
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    Python实现发送邮件的功能报错

    你好!根据你的问题描述,你的代码中实现了发送邮件的功能,但是出现了报错。下面是可能的解决方案:

    1. 检查邮件服务器设置:确保你的邮件服务器设置正确,包括服务器地址、端口号、用户名和密码等。
    2. 检查邮件内容:检查邮件内容是否正确,包括主题、正文和附件等。
    3. 检查Python邮件库:检查Python邮件库是否正确安装和配置,包括smtplibemail模块等。

    以下是一个简单的Python邮件发送示例代码:

    import smtplib
    from email.mime.text import MIMEText
    
    def send_email(subject, msg, from_addr, to_addr, smtp_server, smtp_port, smtp_user, smtp_pass):
        msg = MIMEText(msg)
        msg['Subject'] = subject
        msg['From'] = from_addr
        msg['To'] = to_addr
    
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.login(smtp_user, smtp_pass)
        server.sendmail(from_addr, to_addr, msg.as_string())
        server.quit()
    
    # 使用示例
    subject = "Test Email"
    msg = "Hello, this is a test email."
    from_addr = "your_email@example.com"
    to_addr = "recipient_email@example.com"
    smtp_server = "smtp.example.com"
    smtp_port = 587
    smtp_user = "your_email@example.com"
    smtp_pass = "your_password"
    
    send_email(subject, msg, from_addr, to_addr, smtp_server, smtp_port, smtp_user, smtp_pass)
    

    请注意,这只是一个简单的示例代码,你需要根据你的实际情况进行修改和配置。

    如果你仍然无法解决问题,请提供更多的错误信息和代码,我将尽力帮助你解决问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月7日