python发送邮件outlook
时间: 2024-02-20 10:55:16 浏览: 91
在Python中发送邮件可以使用smtplib库来现,下面是一个使用Outlook发送邮件的示例:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def send_email(subject, content, sender, password, receiver):
设置邮件内容
message = MIMEText(content, 'plain', 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
message['From'] = sender
message['To'] = receiver
try:
# 连接服务器
smtpObj = smtplib.SMTP('smtp.office365.com 587)
smtpObj.starttls()
# 登录邮箱
smtpObj.login(sender, password)
# 发送邮件
smtpObj.sendmail(sender receiver, message.as_string())
print("邮发送成功")
except smtplib.SMTPExcept as e:
print("邮件发送失败:" + str(e))
finally:
# 关闭连接
smtpObj.quit()
# 使用示例
subject = "测试邮件"
content = "这是一封测试邮件"
sender = "your_email@outlook.com"
password = "your_password"
receiver = "recipient_email@example.com"
send_email(subject, content, sender, password, receiver)
```
请注意,上述代码中的`sender`和`password`需要替换为你自己的Outlook邮箱地址和密码,`receiver`需要替换为接收邮件的邮箱地址。
阅读全文