python發送outlook郵件代碼
时间: 2023-10-15 15:29:09 浏览: 23
您可以使用Python的`smtplib`库来发送Outlook邮件。以下是一个简单的示例代码:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 您的 Outlook 邮箱凭据
sender_email = "your_email@outlook.com"
sender_password = "your_password"
# 收件人的电子邮件地址
receiver_email = "recipient@example.com"
# 创建包含邮件内容的 MIMEMultipart 对象
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "Python 发送 Outlook 邮件"
# 邮件正文
body = "这是一封通过 Python 发送的邮件"
message.attach(MIMEText(body, "plain"))
# 创建 SMTP 会话并登录到 Outlook 邮件服务器
smtp_server = "smtp-mail.outlook.com"
smtp_port = 587
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
# 发送邮件
server.send_message(message)
print("邮件发送成功")
```
请确保将`your_email@outlook.com`替换为您的 Outlook 邮箱地址,将`your_password`替换为您的邮箱密码,将`recipient@example.com`替换为收件人的邮箱地址。此外,您可能需要根据您的 Outlook 邮件服务器进行其他设置调整(如端口号、SSL/TLS 加密等)。
注意:在使用此代码之前,确保您已经安装了 Python 的`smtplib`库。您可以使用以下命令通过 pip 进行安装:
```
pip install secure-smtplib
```