如何用Python实现与Office365邮箱的交互示例?

2026-05-05 10:381阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计564个文字,预计阅读时间需要3分钟。

如何用Python实现与Office365邮箱的交互示例?

一、概述最近遇到一个需求,需要使用Office365邮箱发送邮件,但使用SSL发送会失败,必须使用TLS加密协议才能成功发送。

二、完整代码pythonimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipart

def send_email(subject, body, to_email, from_email, password): msg=MIMEMultipart() msg['From']=from_email msg['To']=to_email msg['Subject']=subject

msg.attach(MIMEText(body, 'plain'))

server=smtplib.SMTP('smtp.office365.com', 587) server.starttls() server.login(from_email, password) text=msg.as_string() server.sendmail(from_email, to_email, text) server.quit()

使用示例send_email('Test Subject', 'This is a test email.', 'recipient@example.com', 'sender@example.com', 'password')

如何用Python实现与Office365邮箱的交互示例?

一、概述

最近遇到一个需求,需要使用office365邮箱发送邮件,使用SSL发送会失败,必须使用TLS加密协议才能发送成功。

二、完整代码

使用类封装了一下,功能如下:

1. 支持附件

2. 支持多个发件人

3. 执行TLS

MailTools.py

#!/usr/bin/env python3 # coding: utf-8 import smtplib # 加载smtplib模块 from email.mime.text import MIMEText from email.utils import formataddr from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication import time class SendMail(object): def __init__(self,sender,title,content): self.sender = sender #发送地址 self.title = title # 标题 self.content = content # 发送内容 self.sys_sender = 'xx@office365.com' # 系统账户 self.sys_pwd = '123456' # 系统账户密码 def send(self,file_list): """ 发送邮件 :param file_list: 附件文件列表 :return: bool """ try: # 创建一个带附件的实例 msg = MIMEMultipart() # 发件人格式 msg['From'] = formataddr(["", self.sys_sender]) # 收件人格式 msg['To'] = formataddr(["", self.sender]) # 邮件主题 msg['Subject'] = self.title # 邮件正文内容 msg.attach(MIMEText(self.content, 'plain', 'utf-8')) # 多个附件 for file_name in file_list: print("file_name",file_name) # 构造附件 xlsxpart = MIMEApplication(open(file_name, 'rb').read()) # filename表示邮件中显示的附件名 xlsxpart.add_header('Content-Disposition','attachment',filename = '%s'%file_name) msg.attach(xlsxpart) # SMTP服务器 server = smtplib.SMTP("smtp.office365.com", 587,timeout=10) server.ehlo() server.starttls() # 登录账户 server.login(self.sys_sender, self.sys_pwd) # 发送邮件 server.sendmail(self.sys_sender, [self.sender, ], msg.as_string()) # 退出账户 server.quit() return True except Exception as e: print(e) return False if __name__ == '__main__': # 发送地址 sender = "12345678@qq.com" # 标题 title = "测试告警" # 开始时间 start_time = time.strftime('%Y-%m-%d %H:%M:%S') ip = "xx.xx.xx.xx" # 发送内容 content = "{} ip: {} 掉线".format(start_time,ip) # 附件列表 file_list = [] ret = SendMail(sender, title, content).send(file_list) print(ret,type(ret))

注意:请根据实际情况,修改邮件账号和密码。

以上就是Python 使用office365邮箱的示例的详细内容,更多关于python 使用office邮箱的资料请关注易盾网络其它相关文章!

标签:示例

本文共计564个文字,预计阅读时间需要3分钟。

如何用Python实现与Office365邮箱的交互示例?

一、概述最近遇到一个需求,需要使用Office365邮箱发送邮件,但使用SSL发送会失败,必须使用TLS加密协议才能成功发送。

二、完整代码pythonimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipart

def send_email(subject, body, to_email, from_email, password): msg=MIMEMultipart() msg['From']=from_email msg['To']=to_email msg['Subject']=subject

msg.attach(MIMEText(body, 'plain'))

server=smtplib.SMTP('smtp.office365.com', 587) server.starttls() server.login(from_email, password) text=msg.as_string() server.sendmail(from_email, to_email, text) server.quit()

使用示例send_email('Test Subject', 'This is a test email.', 'recipient@example.com', 'sender@example.com', 'password')

如何用Python实现与Office365邮箱的交互示例?

一、概述

最近遇到一个需求,需要使用office365邮箱发送邮件,使用SSL发送会失败,必须使用TLS加密协议才能发送成功。

二、完整代码

使用类封装了一下,功能如下:

1. 支持附件

2. 支持多个发件人

3. 执行TLS

MailTools.py

#!/usr/bin/env python3 # coding: utf-8 import smtplib # 加载smtplib模块 from email.mime.text import MIMEText from email.utils import formataddr from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication import time class SendMail(object): def __init__(self,sender,title,content): self.sender = sender #发送地址 self.title = title # 标题 self.content = content # 发送内容 self.sys_sender = 'xx@office365.com' # 系统账户 self.sys_pwd = '123456' # 系统账户密码 def send(self,file_list): """ 发送邮件 :param file_list: 附件文件列表 :return: bool """ try: # 创建一个带附件的实例 msg = MIMEMultipart() # 发件人格式 msg['From'] = formataddr(["", self.sys_sender]) # 收件人格式 msg['To'] = formataddr(["", self.sender]) # 邮件主题 msg['Subject'] = self.title # 邮件正文内容 msg.attach(MIMEText(self.content, 'plain', 'utf-8')) # 多个附件 for file_name in file_list: print("file_name",file_name) # 构造附件 xlsxpart = MIMEApplication(open(file_name, 'rb').read()) # filename表示邮件中显示的附件名 xlsxpart.add_header('Content-Disposition','attachment',filename = '%s'%file_name) msg.attach(xlsxpart) # SMTP服务器 server = smtplib.SMTP("smtp.office365.com", 587,timeout=10) server.ehlo() server.starttls() # 登录账户 server.login(self.sys_sender, self.sys_pwd) # 发送邮件 server.sendmail(self.sys_sender, [self.sender, ], msg.as_string()) # 退出账户 server.quit() return True except Exception as e: print(e) return False if __name__ == '__main__': # 发送地址 sender = "12345678@qq.com" # 标题 title = "测试告警" # 开始时间 start_time = time.strftime('%Y-%m-%d %H:%M:%S') ip = "xx.xx.xx.xx" # 发送内容 content = "{} ip: {} 掉线".format(start_time,ip) # 附件列表 file_list = [] ret = SendMail(sender, title, content).send(file_list) print(ret,type(ret))

注意:请根据实际情况,修改邮件账号和密码。

以上就是Python 使用office365邮箱的示例的详细内容,更多关于python 使用office邮箱的资料请关注易盾网络其它相关文章!

标签:示例