Python 使用office365邮箱的示例

作者:py3study 时间:2023-01-10 00:18:17 

一、概述

最近遇到一个需求,需要使用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))

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

来源:https://cloud.tencent.com/developer/article/1642660

标签:python,office365,邮箱
0
投稿

猜你喜欢

  • Python编程源码报错解决方法总结经验分享

    2023-11-16 07:49:46
  • python 除法保留两位小数点的方法

    2022-05-09 02:31:22
  • python实现嵌套列表平铺的两种方法

    2021-06-12 07:20:02
  • ASP实现控制虚拟主机功能的函数ADSI

    2008-10-12 13:12:00
  • JAVASCRIPT实现的WEB页面跳转以及页面间传值方法

    2023-08-23 04:56:09
  • pandas DataFrame 行列索引及值的获取的方法

    2022-03-24 06:14:11
  • wxpython布局的实现方法

    2022-01-31 10:20:57
  • python3.10及以上版本编译安装ssl模块的详细过程

    2022-05-05 07:26:28
  • Python 分支结构详解

    2021-03-17 01:43:06
  • Python的Django框架中自定义模版标签的示例

    2022-10-31 22:28:56
  • Javascript的错还是浏览器的问题——2009年为何显示为109年

    2009-01-11 18:19:00
  • Python3.8.2安装包及安装教程图文详解(附安装包)

    2021-11-17 19:35:14
  • Python的numpy库中将矩阵转换为列表等函数的方法

    2021-06-19 12:18:21
  • 如何提示用户打开Cookie?

    2010-06-07 20:53:00
  • Python 功能和特点(新手必学)

    2022-02-23 04:11:25
  • python面向对象编程设计原则之单一职责原则详解

    2022-02-13 20:51:25
  • JavaScript Length 字符长度函数

    2008-12-12 12:29:00
  • python实现定时自动备份文件到其他主机的实例代码

    2023-04-26 09:30:52
  • python实现哈希表

    2022-12-16 09:00:19
  • 浅谈为什么#{}可以防止SQL注入

    2024-01-27 07:19:08
  • asp之家 网络编程 m.aspxhome.com