1、利用python自带的第三方库smtplib发送邮件,不带附件

import smtplib
from email.mime.text import MIMEText
from email.header import Header# 发送邮件标题
subject = 'Python email test'# 编写HTML类型的邮件正文
msg = MIMEText('<html><h1>你好</h1></html>', 'html', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = '发送过去显示的名称<发件人邮箱>'
msg['To'] = "收件人邮箱"# 连接发送邮件
smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("发件人", "授权码")
smtp.sendmail("发件人","收件人", msg.as_string())smtp.quit()

2、smtplib发送邮件,带附件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 邮件主题
subject = 'Python send email test'
# 发送的附件
with open('log.txt', 'rb') as f:send_att = f.read()att = MIMEText(send_att, 'text', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="log.txt"'msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = '发送过去显示的名称<发件人邮箱>'
msg['To'] = "收件人邮箱"
msg.attach(att)smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("发件人", "授权码")
smtp.sendmail("发件人","收件人", msg.as_string())
smtp.quit()

2、利用yagmail发送

import yagmail#连接邮箱服务器
yag = yagmail.SMTP(user="发件人", password="授权码", host='smtp.126.com')# 邮箱正文
contents = ['This is a mail that is sent automatically. ']# 发送邮件
yag.send(["收件人1","收件人2"], "主题", contents,["附件1","附件2"])
#多个收件人多个附件用列表形式

3、selenium模拟126邮箱发送邮件

from selenium import webdriver
import time
class Mail:def __init__(self,driver):self.driver=driverdef open(self):self.driver.get("http://www.126.com")self.driver.maximize_window()def click_switchAccountLogin(self):try:self.driver.find_element_by_id("switchAccounLogin").click()  ###点击密码登录,有时候可能不出现这个框except:passdef quit(self):self.driver.quit()def login(self,username,password):'''登录:param username::param password::return:'''login_frame = self.driver.find_element_by_css_selector('iframe[id^="x-URS-iframe"]')self.driver.switch_to.frame(login_frame)self.driver.find_element_by_name("email").clear()self.driver.find_element_by_name("email").send_keys(username)self.driver.find_element_by_name("password").clear()self.driver.find_element_by_name("password").send_keys(password)self.driver.find_element_by_id("dologin").click()self.driver.switch_to.default_content()def logout(self):'''退出:return:'''self.driver.find_element_by_link_text('退出').click()def send_mail_without_attachment(self,receiver,subject,content):'''发送不带附件的邮件:param receiver::param subject::param content::return:'''self.click_write_email()time.sleep(1)mail.input_toWho(receiver)time.sleep(1)mail.input_subject(subject)time.sleep(1)mail.input_content(content)time.sleep(1)mail.click_send()def send_mail_with_attachment(self,receiver,subject,attachment,content):'''发送带附件的邮件:param receiver::param attachment::param subject::param content::return:'''self.click_write_email()time.sleep(1)mail.input_toWho(receiver)time.sleep(1)mail.input_subject(subject)time.sleep(1)mail.upload_attachment(attachment)time.sleep(1)mail.input_content(content)time.sleep(1)mail.click_send()def click_write_email(self):'''点击写信:return:'''self.driver.find_element_by_xpath("//li[contains(@id,'_mail_component')]//span[.='写 信']").click()def input_toWho(self,receiver):'''输入收件人:param receiver::return:'''self.driver.find_element_by_xpath("//input[@class='nui-editableAddr-ipt']").send_keys(receiver)def input_subject(self,subject):'''输入主题:param subject::return:'''self.driver.find_element_by_xpath("//input[contains(@id,'subjectInput')]").send_keys(subject)def input_content(self,text):'''切换表单iframe,输入正文:param text::return:'''self.driver.switch_to.frame(self.driver.find_element_by_xpath('//iframe[@class="APP-editor-iframe"]'))self.driver.find_element_by_xpath('//body[@class="nui-scroll"]').send_keys(text)self.driver.switch_to.default_content()def click_send(self):'''点击发送:return:'''self.driver.find_element_by_xpath("//footer[contains(@class,'jp')]/div[@role='button']/span[.='发送']").click()def upload_attachment(self,attachment):'''上传附件:param attachment::return:'''upload = self.driver.find_element_by_xpath('//input[@type="file"]')upload.send_keys(attachment)if __name__== "__main__":username="发件人账号"password="密码"receiver="收件人"subject="主题"attachment='附件'content="正文"driver=webdriver.Chrome()driver.implicitly_wait(10)mail=Mail(driver)mail.open()mail.click_switchAccountLogin()time.sleep(1)mail.login(username,password)time.sleep(1)mail.send_mail_with_attachment(receiver,subject,attachment,content)time.sleep(2)mail.logout()mail.quit()

##参考虫师著 Selenium3自动化测试实战 基于python 语言。

python+selenium3登录126邮箱并发送邮件相关推荐

  1. python+selenium 登录126邮箱

    之前尝试用requests.post的办法登录126邮箱,但是126的邮箱在传递密码是是实时加密,没有解决掉实时加密的问题,因此失败: 最近在学习selenium,发现用selenium后登录126邮 ...

  2. python+selenium3解决126邮箱登录出现验证码问题

    研究了好几天,终于解决了126邮箱登录出现的验证码问题!!! 首先需要导包: 1.下载PIL,一个强大的处理图像的库.无法使用pip小工具下载,而且官网没有对应的python3版本,不过可以使用pil ...

  3. python+selenium自动登录126邮箱并发送邮件

    # 代码拿来就能使用,只需更改账号.密码和收件人邮箱:未封装import time from selenium import webdriver from selenium.webdriver.com ...

  4. python 自动登录方法_Python自动登录126邮箱的方法

    本文实例讲述了Python自动登录126邮箱的方法.分享给大家供大家参考.具体实现方法如下: import sys, urllib2, urllib,cookielib import re cooki ...

  5. selenium3测试126邮箱登录日志

    selenium3测试126邮箱登录日志 测试邮箱的登录和退出功能 from time import sleep from selenium import webdriver from MailCla ...

  6. Python+Selenium自动化测试——126邮箱自动登录脚本(登录首页是二维码,切入账号密码输入框)

    Python+Selenium自动化测试--126邮箱自动登录脚本 版权声明:本文为博主原创文章,未经允许不得转载.https://blog.csdn.net/qiao_wan/article/det ...

  7. python自动注册邮箱_python2+selenium+mail,自动登录126邮箱

    在进行登录126邮箱时有几个坑,要完美避过可以看一下下文,直接上代码: #encoding = utf-8 from selenium import webdriver import unittest ...

  8. 11.selenium登录126邮箱出现定位问题解决

    在用selenium登录126邮箱的时候会发现在定位到登录密码那里就会有出现问题了,一直定位不进去,接下来通过2个用例完成登录及发送邮件功能的实现方法 一.登录用例 在登录的时候会出现定位不到账号和密 ...

  9. 15_Python3.6+selenium2.53.6自动化测试_登录126邮箱

    一.实现功能:成功登录126邮箱 1 打开126邮箱 2 切换iframe 3 输入用户名.密码,点击登录 3 切回,退出浏览器 二.实现代码 # -*- coding: utf-8 -*- ''' ...

最新文章

  1. 使用T-SQL语句操作数据表-更新数据
  2. html dom节点类型,浅谈Javascript中的12种DOM节点类型
  3. Spring中配置Quartz的misfireInstruction
  4. [LeetCode] Linked List Cycle
  5. 关于看到了很多的短视频之后的思考
  6. web.config总结
  7. rest post put_REST / HTTP方法:POST与PUT与PATCH
  8. VS2010 自动化整理代码(1)--- VS正则表达替换 PK Vim
  9. 以前的报表都白做了!app上做可视化数据分析,这个方法太强了
  10. 一加7 Pro详细配置规格曝光:售价妥妥破5000
  11. Java中Date, Calendar, SimpleDateFormat的相互转换
  12. 【大数据-第二期】java基础第五天作业
  13. 带你进入 jBPM 工作流的世界
  14. Windows虚拟设备驱动开发总结
  15. linux中命令du -sm,Linux中du命令使用介绍
  16. 邵阳职院计算机专业怎么样,邵阳市古峰职业学校怎么样?
  17. Feb14 小白《Linux就该这么学》学习笔记1
  18. 孤独的灵魂 - 复旦投毒案
  19. 当动作捕捉遇上圣诞节,原来如此有趣
  20. Ribbon负载均衡策略初步解读

热门文章

  1. plantuml介绍与使用
  2. [RK3399][Android7.1]触摸屏TP,敦泰FT5436驱动移植
  3. Android开发工具类
  4. 自定义alert和confirm的效果,个人感觉蛮好使用的
  5. SQL注入之floor报错注入
  6. html5 bdi 不起作用,html bdi标签的使用详解
  7. 网盾极风云:五分钟搞懂HTTP和HTTPS
  8. 机器学习模型 非线性模型_pycaret在几分钟内准备好您的机器学习模型
  9. 求两点之间最短路径-Dijkstra算法
  10. 海尔微型计算机云悦t3G276ia,没了海尔云悦miniA 迷你主机界尽失半壁江山