Python脚本IMAP登陆邮箱 搜索邮件
功能:python 脚本登陆邮箱 查找 指定邮件。
代码:
#!/usr/bin/env python
#-*- coding:UTF-8 -*-
# 
# pythont  version: 3.8.8
## 导入模块
import imaplib,string,os,email
from email.parser import Parser
from email.header import decode_header
## 登陆信息
username = 'gmailusername'
password = 'gmailpassword'
## 登陆邮箱
### 登陆,方式一:
#i = imaplib.IMAP4_SSL("192.168.11.22")      ## 服务器IP
#i.login("test@bai.com","aaabbb")    ## 邮箱地址 和 邮箱密码
### 登陆,方式二:
i.imaplib.IMAP4_SSL('imap.gmail.com', 993)	## 服务器IP,端口
i.login(username, password)    ## 邮箱地址 和 邮箱密码
## 查看“收件箱”有多少封邮件
print("--- 查询收件箱有多少封邮件")
# INBOX(默认收件箱)/Drafts(草稿箱)/Junk(垃圾箱)/Trash(已删除)/Sent(Sent Items)(已发送)/UNSEEN(未读)
INBOX_count  = i.select('INBOX')    ## 收件箱有多少封邮件,INBOX不区分大小写
print(INBOX_count)	输出:<'OK', [b'108']> 
## 搜索指定标题的邮件
### 搜索邮件标题包含‘tels’字样的邮件,下面这样写只能搜英文标题
msgtyp,msgitem=i.search(None,‘Subject’,'tels')
### 搜索邮件标题包含‘7月’字样的邮件,中文标题是 'utf-8' 编码的
msgtyp,msgitem=i.search(None,‘Subject’,'7月'.encode('utf-8'))
### 搜索邮件标题包含‘7月’字样的邮件,中文标题是 'gb2312' 编码的
msgtyp,msgitem=i.search(None,‘Subject’,'7月'.encode('gb2312'))
print(msgitem[0])   ## 输出:<'OK', [b'108']>  --> b'108'
print(type(msgitem[0]))   ## 输出:<class 'bytes'>
## 退出邮箱登陆,关闭连接
i.logout()	
-
- IMAP4 类的实例变量的方法列表如下:
- fetch(message_set, message_parts):取出信息。
- login(user, password):登录 IMAP4 服务器。
- logout():注销 IMAP4 服务器,关闭连接。
- search(charset, criterium [, ...]):搜索邮件信箱找出符合的信息。
- select([mailbox [, readonly]]):选择一个邮件信箱。
未完,待续
-
参考:
http://www.zzvips.com/article/78884.html
https://www.cnblogs.com/leejay/category/1916925.html
https://www.cnblogs.com/leejay/p/14281725.html
https://qa.1r1g.com/sf/ask/2574669541/
https://www.shuzhiduo.com/A/QV5Zn2O65y/
https://www.cnblogs.com/liqiongming/p/14549134.html
https://www.weixueyuan.net/a/750.html
https://blog.csdn.net/hans99812345/article/details/114649737
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号