一、说明

1.1 程序说明

(1)smtp是邮件发送协议;pop和imap都是邮件接收协议,两者的区别通常的说法是imap的操作会同步到邮箱服务器而pop不会,表现上我也不是很清楚

(2)本程序实现使用smtplib标准库实现邮件发送、使用poplib标准库和imaplib标准库实现邮件收取

(3)具体到代码上,三个功能依次对应程序中的send_email_by_smtp()、recv_email_by_pop3()、recv_email_by_imap4()三个函数,这三个函数相互独立没有调用关系

(4)由于还没弄清楚要怎么能很好地解码邮件,所以这里的pop和imap都只是直接将最新的一封邮件读取后直接打印出来,并没有进行解码。

(5)在贴上代码时,代码中的邮箱已全部替换,使用时记得修改这些信息;注释中已都有较详细说明,不多辍述。

(6)对于自己邮箱的smtp服端器、pop服务器、imap服务器地址如果不知道则自己百度一下,一般都是“协议+邮箱后辍”的形式(比如pop.qq.com),这种形式如果能ping通一般就是了;端口则可能多变,如果查不到就直接nmap等工具扫一下。比如下面:

1.2 程序运行截图

smtp邮件发送截图:

pop3邮件收取截图:

imap4邮件收取截图:

二、程序代码

import smtplib
import poplib
import imaplib
from email.mime.text import MIMEText
from email.header import Header class operate_email:
# 此函数通过使用smtplib实现发送邮件
def send_email_by_smtp(self):
# 用于发送邮件的邮箱。修改成自己的邮箱
sender_email_address = "your_email@qq.com"
# 用于发送邮件的邮箱的密码。修改成自己的邮箱的密码
sender_email_password = "your_email_password"
# 用于发送邮件的邮箱的smtp服务器,也可以直接是IP地址
# 修改成自己邮箱的sntp服务器地址;qq邮箱不需要修改此值
smtp_server_host = "smtp.qq.com"
# 修改成自己邮箱的sntp服务器监听的端口;qq邮箱不需要修改此值
smtp_server_port = 465
# 要发往的邮箱
receiver_email = "your_dest_email@qq.com"
# 要发送的邮件主题
message_subject = "Python smtp测试邮件"
# 要发送的邮件内容
message_context = "这是一封通过Python smtp发送的测试邮件..." # 邮件对象,用于构建邮件
# 如果要发送html,请将plain改为html
message = MIMEText(message_context, 'plain', 'utf-8')
# 设置发件人(声称的)
message["From"] = Header(sender_email_address, "utf-8")
# 设置收件人(声称的)
message["To"] = Header(receiver_email, "utf-8")
# 设置邮件主题
message["Subject"] = Header(message_subject,"utf-8") # 连接smtp服务器。如果没有使用SSL,将SMTP_SSL()改成SMTP()即可其他都不需要做改动
email_client = smtplib.SMTP_SSL(smtp_server_host, smtp_server_port)
try:
# 验证邮箱及密码是否正确
email_client.login(sender_email_address, sender_email_password)
print("smtp----login success, now will send an email to {receiver_email}")
except:
print("smtp----sorry, username or password not correct or another problem occur")
else:
# 发送邮件
email_client.sendmail(sender_email_address, receiver_email, message.as_string())
print(f"smtp----send email to {receiver_email} finish")
finally:
# 关闭连接
email_client.close() # 此函数通过使用poplib实现接收邮件
def recv_email_by_pop3(self):
# 要进行邮件接收的邮箱。改成自己的邮箱
email_address = "your_email@qq.com"
# 要进行邮件接收的邮箱的密码。改成自己的邮箱的密码
email_password = "your_email_password"
# 邮箱对应的pop服务器,也可以直接是IP地址
# 改成自己邮箱的pop服务器;qq邮箱不需要修改此值
pop_server_host = "pop.qq.com"
# 邮箱对应的pop服务器的监听端口。改成自己邮箱的pop服务器的端口;qq邮箱不需要修改此值
pop_server_port = 995 try:
# 连接pop服务器。如果没有使用SSL,将POP3_SSL()改成POP3()即可其他都不需要做改动
email_server = poplib.POP3_SSL(host=pop_server_host, port=pop_server_port, timeout=10)
print("pop3----connect server success, now will check username")
except:
print("pop3----sorry the given email server address connect time out")
exit(1)
try:
# 验证邮箱是否存在
email_server.user(email_address)
print("pop3----username exist, now will check password")
except:
print("pop3----sorry the given email address seem do not exist")
exit(1)
try:
# 验证邮箱密码是否正确
email_server.pass_(email_password)
print("pop3----password correct,now will list email")
except:
print("pop3----sorry the given username seem do not correct")
exit(1) # 邮箱中其收到的邮件的数量
email_count = len(email_server.list()[1])
# 通过retr(index)读取第index封邮件的内容;这里读取最后一封,也即最新收到的那一封邮件
resp, lines, octets = email_server.retr(email_count)
# lines是邮件内容,列表形式使用join拼成一个byte变量
email_content = b'\r\n'.join(lines)
# 再将邮件内容由byte转成str类型
email_content = email_content.decode()
print(email_content) # 关闭连接
email_server.close() # 此函数通过使用imaplib实现接收邮件
def recv_email_by_imap4(self):
# 要进行邮件接收的邮箱。改成自己的邮箱
email_address = "your_email@qq.com"
# 要进行邮件接收的邮箱的密码。改成自己的邮箱的密码
email_password = "your_email_password"
# 邮箱对应的imap服务器,也可以直接是IP地址
# 改成自己邮箱的imap服务器;qq邮箱不需要修改此值
imap_server_host = "imap.qq.com"
# 邮箱对应的pop服务器的监听端口。改成自己邮箱的pop服务器的端口;qq邮箱不需要修改此值
imap_server_port = 993 try:
# 连接imap服务器。如果没有使用SSL,将IMAP4_SSL()改成IMAP4()即可其他都不需要做改动
email_server = imaplib.IMAP4_SSL(host=imap_server_host, port=imap_server_port)
print("imap4----connect server success, now will check username")
except:
print("imap4----sorry the given email server address connect time out")
exit(1)
try:
# 验证邮箱及密码是否正确
email_server.login(email_address,email_password)
print("imap4----username exist, now will check password")
except:
print("imap4----sorry the given email address or password seem do not correct")
exit(1) # 邮箱中其收到的邮件的数量
email_server.select()
email_count = len(email_server.search(None, 'ALL')[1][0].split())
# 通过fetch(index)读取第index封邮件的内容;这里读取最后一封,也即最新收到的那一封邮件
typ, email_content = email_server.fetch(f'{email_count}'.encode(), '(RFC822)')
# 将邮件内存由byte转成str
email_content = email_content[0][1].decode()
print(email_content)
# 关闭select
email_server.close()
# 关闭连接
email_server.logout() if __name__ == "__main__":
# 实例化
email_client = operate_email()
# 调用通过smtp发送邮件的发送函数
email_client.send_email_by_smtp()
# 调用通过pop3接收邮件的接收函数
email_client.recv_email_by_pop3()
# 调用通过imap4接收邮件的接收函数
email_client.recv_email_by_imap4()

参考:

https://docs.python.org/3/library/smtplib.html

https://docs.python.org/3/library/poplib.html

https://docs.python.org/3/library/imaplib.html

http://help.163.com/10/0203/13/5UJONJ4I00753VB8.html

http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371

http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=331

Python3+smtplib+poplib+imaplib实现发送和收取邮件(以qq邮箱为例)的更多相关文章

  1. thinkphp5 邮件发送(qq邮箱为例)

    一.首先需要你开启smtp 服务,登录qq邮箱,进入设置 -> 账户 注意: 开启后会生成授权码,一定要记下,两个都记下,登录邮件客户端需要.这里配置邮件发送也需要这个授权码 二. 下载phpm ...

  2. 【故障公告】SendCloud 邮件发送服务故障造成大量 QQ 邮箱收不到邮件

    抱歉,由于我们所使用的搜狐旗下的 SendCloud 邮件发送服务出现故障,今天上午大量发往 @qq.com 邮箱的邮件无法正常发送,从 SendCloud 管理控制台看这些邮件一直处于“请求中”的状 ...

  3. 命令行发送SMTP协议邮件(163邮箱)

    这里我们用163邮箱为例子,借助命令行发送smtp邮件 1.连接服务器 在终端上输入:telnet smtp.163.com 25 回车,然后就连接了服务器的25端口,成功会输出 220 163.co ...

  4. 解决使用Foxmail客户端软件不能收取腾讯企业邮箱的全部邮件

    一般说来,使用Foxmail客户端软件收取邮箱时,需要作如下几步: 1.进入邮箱web界面授权开启POP3/SMTP服务.IMAP/SMTP等服务 2.在邮箱web界面配置收取选项,可选择收取全部邮件 ...

  5. Thinkphp3.2 PHPMailer 发送 QQ邮箱 163邮箱

    在进入正题这前先看下网易(163)邮箱的服务器地址和端口号 类型 服务器名称 服务器地址 SSL协议端口号 非SSL协议端口号 收件服务器 POP pop.163.com 995 110 收件服务器 ...

  6. SpringBoot系列(十四)集成邮件发送服务及邮件发送的几种方式

    往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)web静 ...

  7. 【python3】基于 qq邮箱的邮件发送

    脚本内容: #!/usr/bin/python3 # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText ...

  8. 使用Python内置的smtplib包和email包来实现邮件的构造和发送。

    此文章github地址:https://github.com/GhostCNZ/Python_sendEmail Python_sendEmail 使用Python内置的smtplib包和email包 ...

  9. python3:利用SMTP协议发送QQ邮件+附件

    转载请表明出处:https://www.cnblogs.com/shapeL/p/9115887.html 1.发送QQ邮件,首先必须知道QQ邮箱的SMTP服务器 http://service.mai ...

随机推荐

  1. vue--v-model表单控件绑定

    原文链接:https://www.cnblogs.com/dyfbk/p/6868350.html v-model 指令在表单控件元素上创建双向数据绑定,下面一一进行示例解释. 1.v-model 双 ...

  2. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  3. 两个DIV并排显示

    今天做的一个项目,需要做3个div,一个是总框(Div1),另外两个是子框,按比例填满div1,我设置好两个div的width和height,发现效果是两个子div上下显示,如图所示: 要想将两个DI ...

  4. Appium-desktop的下载&安装

    下载地址: http://appium.io/ 选择版本 双击安装

  5. 负数字符串经过int处理之后还是负数

    <?php $v = '-1'; $b = (int)$v; echo $b;

  6. PostegreSQL模板数据库

    模板数据库 模板数据库就是创建新database时,PostgreSQL会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件. CREATE DATABASE 实际上是通过拷贝一个现有的 ...

  7. SqlServer中exists和in的区别

    1.in 2.exists

  8. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  9. Pychram - 使用介绍

    Pychram - 使用介绍 PyCharm中Directory与Python package的区别 对于Python而言,有一点是要认识明确的,python作为一个相对而言轻量级的,易用的脚本语言( ...

  10. vue 导出excel表格

    对于涉及到数据比较多的管理后台导出excel 表格这个需求就非常的常见了 所以? vue 怎么到处excel表格的? 有两种办法 1:请求接口后台直接给你的是excel文件,你需要做的就是怎么接收ex ...