此代码亲测可用!!!
一、发送、接收邮件所用到的pop3、imap协议
https://blog.csdn.net/iarabb/article/details/104423584
二、在qq中开启pop3、imap服务
1、在qq邮箱中,找到设置–》账号:
注意:
1、pop3, imap都一定要打开,才可以实现收发邮件!!!!
2、在刚开通后,去运行脚本,可能会出现能够登录,但是无法读取邮件的权限错误,这个问题目前猜测是因为腾讯后台更改需要时间,过大概10分钟再去执行就不会报相应的问题
三、发送qq邮件代码
可发文件
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 写成了一个通用的函数接口,想直接用的话,把参数的注释去掉就好
def send_email(msg_from, passwd, msg_to, text_content, file_path=None):
msg = MIMEMultipart()
subject = "python 实现邮箱发送邮件" # 主题
text = MIMEText(text_content)
msg.attach(text)
# docFile = 'C:/Users/main.py' 如果需要添加附件,就给定路径
if file_path: # 最开始的函数参数我默认设置了None ,想添加附件,自行更改一下就好
docFile = file_path
docApart = MIMEApplication(open(docFile, 'rb').read())
docApart.add_header('Content-Disposition', 'attachment', filename=docFile)
msg.attach(docApart)
print('发送附件!')
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print("发送成功")
except smtplib.SMTPException as e:
print("发送失败")
finally:
s.quit()
if __name__ == '__main__':
msg_from = '******@qq.com' # 发送方邮箱
passwd = '****************' # 填入发送方邮箱的授权码(就是刚刚你拿到的那个授权码)
msg_to = '*******@qq.com' # 收件人邮箱
text_content = "hi,this is a demo!" # 发送的邮件内容
file_path = 'ceshi.xlsx' # 需要发送的附件目录
send_email(msg_from,passwd,msg_to,text_content,file_path)
四、接收qq邮件代码实例
可收文件
import poplib
# 解析邮件
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
# 解析消息头中的字符串
# 没有这个函数,print出来的会使乱码的头部信息。如'=?gb18030?B?yrXWpL3hufsueGxz?='这种
# 通过decode,将其变为中文
def decode_str(s):
value, charset = decode_header(s)[0]
if charset:
value = value.decode(charset)
return value
# 解码邮件信息分为两个步骤,第一个是取出头部信息
# 首先取头部信息
# 主要取出['From','To','Subject']
'''
From: "=?gb18030?B?anVzdHpjYw==?=" <justonezcc@sina.com>
To: "=?gb18030?B?ztLX1Ly6tcTTys/k?=" <392361639@qq.com>
Subject: =?gb18030?B?dGV4dMTjusM=?=
'''
# 如上述样式,均需要解码
def get_header(msg):
for header in ['From', 'To', 'Subject']:
value = msg.get(header, '')
if value:
# 文章的标题有专门的处理方法
if header == 'Subject':
value = decode_str(value)
elif header in ['From', 'To']:
# 地址也有专门的处理方法
hdr, addr = parseaddr(value)
name = decode_str(addr)
# value = name + ' < ' + addr + ' > '
value = name
print(header + ':' + value)
# 头部信息已取出
# 获取邮件的字符编码,首先在message中寻找编码,如果没有,就在header的Content-Type中寻找
def guess_charset(msg):
charset = msg.get_charset()
if charset is None:
content_type = msg.get('Content-Type', '').lower()
pos = content_type.find('charset=')
if pos >= 0:
charset = content_type[pos + 8:].strip()
return charset
# 邮件正文部分
# 取附件
# 邮件的正文部分在生成器中,msg.walk()
# 如果存在附件,则可以通过.get_filename()的方式获取文件名称
def get_file(msg):
for part in msg.walk():
filename = part.get_filename()
if filename != None: # 如果存在附件
filename = decode_str(filename) # 获取的文件是乱码名称,通过一开始定义的函数解码
data = part.get_payload(decode=True) # 取出文件正文内容
# 此处可以自己定义文件保存位置
path = filename
f = open(path, 'wb')
f.write(data)
f.close()
print(filename, 'download')
def get_content(msg):
for part in msg.walk():
content_type = part.get_content_type()
charset = guess_charset(part)
# 如果有附件,则直接跳过
if part.get_filename() != None:
continue
email_content_type = ''
content = ''
if content_type == 'text/plain':
email_content_type = 'text'
elif content_type == 'text/html':
print('html 格式 跳过')
continue # 不要html格式的邮件
email_content_type = 'html'
if charset:
try:
content = part.get_payload(decode=True).decode(charset)
except AttributeError:
print('type error')
except LookupError:
print("unknown encoding: utf-8")
if email_content_type == '':
continue
# 如果内容为空,也跳过
print(email_content_type + ' ----- ' + content)
# get_file(msg)
if __name__ == '__main__':
email = '******@qq.com'
password = '*********'
server = poplib.POP3_SSL('pop.qq.com')
server.user(email)
server.pass_(password)
# 登录的过程
# resp, mails, octets = server.list()
mails, totalsize = server.stat()
# index = len(mails) # 邮件的总数
index = mails
# 此处的循环是取最近的几封邮件
for i in range(index - 2, index + 1):
resp, lines, octets = server.retr(i) # 取邮件
msg_content = b'\r\n'.join(lines).decode('utf-8', 'ignore')
msg = Parser().parsestr(msg_content)
# server.dele(index) 删除邮件
get_header(msg)
get_file(msg)
get_content(msg)
server.quit()
总结:在使用的过程中,由于没有将pop3,imap都开启,导致接收邮件时一直登陆不了,所以切记,切记,两个都要开启!!!!!!!!!!!