python - Office365 的 SMTP 电子邮件

标签 python smtp smtplib

我想通过python发送电子邮件,我按照this中的以下代码进行操作链接:

import smtplib

mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls()
mailserver.login('myemail@company.com', 'mypassword')
msg = ('this is a message')
mailserver.sendmail('myemail@company.com','receiver@company.com',msg)

问题: 该电子邮件位于我的发件箱和收件人的收件箱中,但没有文本。它是空的。

没有错误或输出,脚本只是运行,所以我不确定从哪里开始排除故障,因为我不是这方面的专家;谁能解释一下为什么没有消息/文本?

最佳答案

您需要在 sendmail 的第三个参数中的主题和电子邮件正文之间添加 \n

msg = 'Subject: Email Subject.\n{}'.format('this is a message')
mailserver.sendmail('myemail@company.com','receiver@company.com', msg)

关于python - Office365 的 SMTP 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56294982/

相关文章:

python - 计算 Pandas 中特定列和每一行的非零百分比

python - `eb init` 因 pkg_resources.ContextualVersionConflict PyYAML 4.1 而失败

python - 更改 matplotlib 中注释箭头的宽度

python - Cursor.execute数据添加单引号并中断包含Python中mysql的IN(1,2)语句的查询

c# - 邮件不以表格格式发送 C#

用于使用 UTF-8 字符撰写电子邮件的 C 库

django - SMTPDataError at/accounts/signup/(553, b'不允许作为 webmaster@localhost 进行中继')

python - 从 python 中的消息字符串发送电子邮件

python - 从 python 脚本发送电子邮件