来自:https://www.cnblogs.com/sanzangTst/p/8377870.html

发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Header()定义邮件标题。MIMEMulipart模块构造带附件。

Selenium发送邮件流程:

一、网易邮箱

Selenium发送邮件步骤:

1、导入smtplib和email模块;

2、准备发邮件的参数,每个邮箱的发件服务器都不一样,以163为例,百度搜到发件服务器为:smtp.163.com;

3、接下来就是写邮件的主题和正文内容,正文这里用html格式的;

4、最后调用发件服务。

5、参考代码

import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart # 以yeah邮箱为例
# ----------------发件相关参数----------------
smtpserver = 'smtp.163.com'
port = 0
sender = 'sanzang520@yeah.net'
password = 'xxxxxxxxxxxx'
receicer = 'sanzang520@126.com' # ----------------编辑邮件内容----------------
subject = '发送邮件测试'
body = '<p>发送邮件测试Test<p>'
msg = MIMEText(body, 'html', 'UTF-8')
msg['from'] = sender
msg['to'] = receicer
msg['subject'] = subject # ------------------发送邮件-----------------
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender, password)
smtp.sendmail(sender, receicer, msg.as_string())
smtp.quit()

二、腾讯邮箱

Selenium发送邮件步骤:

1、导入smtplib和email模块;

2、腾讯邮箱是需要SSL认证的,找到QQ邮箱授权码,打开QQ邮箱-设置-账号-POP3开启服务-开启;

3、发验证短信获取授权码,照着提示发个短信,如何点我已发送,就会收到授权码;

4、收到授权码后复制,保存下来,这个就可以当QQ邮箱的密码;

5、接下来就是写邮件的主题和正文内容,正文这里用html格式的;

6、最后调用发件服务。

7、参考代码

import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
# 以QQ邮箱为例
# ----------------发件相关参数----------------
smtpserver = 'smtp.qq.com'
port = 0
sender = '2215358510@qq.com'
password = '授权码'
receicer = 'sanzang520@126.com' # ----------------编辑邮件内容----------------
subject = '发送邮件测试'
body = '<p>发送邮件测试Test<p>'
msg = MIMEText(body, 'html', 'UTF-8')
msg['from'] = sender
msg['to'] = receicer
msg['subject'] = subject # ------------------发送邮件-----------------
smtp = smtplib.SMTP_SSL(smtpserver,port)
smtp.login(sender, password)
smtp.sendmail(sender, receicer, msg.as_string())
smtp.quit()

三、同时兼容网易类和腾讯类邮箱

四、多个收件人

1、把receiver参数改成list对象,单个多个都是可以收到的;

2、msg["to"]这个参数不能用list了,得先把receiver参数转化成字符串。

五、发送附件

六、参考代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : chen
# @File : c.py
# @Software: PyCharm
import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
# 同时兼容网易类和腾讯类邮箱
# ----------------发件相关参数----------------
smtpserver = 'smtp.qq.com'
port = 0
sender = '2215358510@qq.com'
password = '授权码'
receicer = ['sanzang520@126.com','sanzang520@yeah.net',] # ----------------编辑邮件内容----------------
subject = '发送邮件测试'
body = '<p>发送邮件测试Test<p>'
msg = MIMEText(body, 'html', 'UTF-8')
msg['from'] = sender
msg['to'] = ';'.join(receicer)
msg['subject'] = subject # 文字部分
part = MIMEText('TEST!!!')
msg.attach(part)
# 附件部分
#---xlsx类型附件---
part = MIMEApplication(open('D:\\test.xlsx','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="test.xlsx")
msg.attach(part)
# jpg类型附件(png类型和jpg一样)
part = MIMEApplication(open('D:\\test.jpg','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="test.jpg")
msg.attach(part)
# pdf类型附件
part = MIMEApplication(open('D:\\test.pdf','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="test.pdf")
msg.attach(part)
# mp3类型附件
part = MIMEApplication(open('D:\\test.mp3','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="test.mp3")
msg.attach(part)
# html类型
part = MIMEText('<html><h1>test!</h1></html>','html','utf-8')
msg.attach(part) # ------------------发送邮件-----------------
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender, password)
except:
smtp = smtplib.SMTP_SSL(smtpserver,port)
smtp.login(sender, password)
smtp.sendmail(sender, receicer, msg.as_string())
smtp.quit()

最新文章

  1. Spring对事务管理的支持的发展历程--转
  2. ActiveMQ与WebSocket的结合应用
  3. ASP.NET 程序中删除文件夹导致session失效解决问题
  4. eclipse项目导入androidstudio
  5. bootstrap部分---网格系统;(几天没写博客了,为了潜心研究一下bootstrap)
  6. angularJs项目实战!02:前端的页面分解与组装
  7. ASP.NET MVC 应用程序的安全性,看一眼你就会了
  8. 不同linux系统添加开机启动程序的命令
  9. C#基础篇01
  10. 每天学点SpringMVC-异常处理
  11. 纯js实现DIV拖拽
  12. 全网搜歌神器Listen1 Mac中文版
  13. nova系列二:kvm介绍
  14. Mysql中的排序规则utf8_unicode_ci、utf8_general_ci总结
  15. Redis持久化存储(AOF与RDB两种模式)
  16. Mongodb查询命令详解
  17. doctype和Quirks模式
  18. PasteDeploy部署Pecan API 服务
  19. eclipse里没有project facets
  20. ansible 使用

热门文章

  1. Spring Boot 系列(九)数据层-集成Spring-data-jpa
  2. 【原创】驱动加载之StartService
  3. spring boot多数据源配置(mysql,redis,mongodb)实战
  4. PHP中的__set和__get方法
  5. python的partial()用法说明
  6. iOS main.m解析
  7. 在AspNetCore中扩展Log系列 - 介绍开源类库的使用(一)
  8. 使用JS模拟锚点跳转
  9. asp.net-基础-20180320
  10. 推荐 3 篇关于 java8 Lambda表达式的文章