【问题标题】:How do I send an email using Python's "MIME handling package"?如何使用 Python 的“MIME 处理包”发送电子邮件?
【发布时间】:2021-12-02 06:41:18
【问题描述】:

这里有关于如何发送电子邮件的文档:https://docs.python.org/3/library/email.examples.html

我尝试运行的完整代码是...

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.message import EmailMessage

# Open the plain text file whose name is in textfile for reading.

# Create a text/plain message
msg = EmailMessage("HI")


# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = "SubjectLine"
msg['From'] = aaaaa@gmail.com
msg['To'] = aaaaa@yahoo.com

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

第 16 行代码出现错误

msg['Subject'] = "SubjectLine"

错误是:'str' object has no attribute 'header_max_count'

【问题讨论】:

  • "第 16 行代码出现错误" ...您确定吗?此行与政策完全无关,符合官方文档的指导。
  • 您能解释一下您认为在EmailMessage("HI") 行中传递给EmailMessage 的论点实际上是做什么的...?您是否阅读了the documentation for EmailMessage 以了解您应该 作为参数传递的内容?您能否分享一下您理解"HI" 在这种情况下是该函数的有效参数的文档?
  • 感谢您的帮助。我将删除这篇文章作为修改我的问题。

标签: python email mime


【解决方案1】:

您似乎对如何实例化EmailMessage 的实例存在误解,并且不清楚您为什么将字符串"HI" 作为参数传递。 The documentation 明确提到此参数旨在获取策略信息,其中将包括它似乎期望的 header_max_count 属性。

在您的代码中删除这种不必要的 "HI" 用法:

msg = EmailMessage()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 2016-11-21
    • 2011-07-19
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2019-10-18
    相关资源
    最近更新 更多