当我在jupyter中运行代码时,我收到了邮件,但我尝试在我的Linux盒子中通过python没有收到任何邮件。
代码如下:
#! /usr/bin/python
import smtplib
import ssl
port = 587
smtp_server = "smtp-mail.outlook.com"
sender = "example@outlook.com"
recipient = "example@outlook.com"
sender_password = "Password"
message = """
Subject: This is a test message
Sent using Python."""
SSL_context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=SSL_context)
server.login(sender, sender_password)
server.sendmail(sender, recipient, message)
您正在尝试连接Outlook与SMTP。您应该要求您的电子邮件帐户允许smtp,这在默认情况下不一定是启用的。
也可以尝试其他基于库的解决方案;
from O365 import Message
html_template = """
<html>
<head>
<title></title>
</head>
<body>
{}
</body>
</html>
"""
final_html_data = html_template.format(df.to_html(index=False))
o365_auth = ('sender_username@company_email.com','Password')
m = Message(auth=o365_auth)
m.setRecipients('receiver_username@company_email.com')
m.setSubject('Weekly report')
m.setBodyHTML(final)
m.sendMessage()
从https://stackoverflow.com/a/52534847/5942941
或安装redmail:
pip install redmail
并尝试使用这个示例;
from redmail import outlook
outlook.user_name = "example@hotmail.com"
outlook.password = "<MY PASSWORD>"
outlook.send(
receivers=["you@example.com"],
subject="An example",
text="Hi, this is an example."
)
https://stackoverflow.com/a/71056577/5942941