Python脚本IMAP登陆邮箱 搜索邮件

功能:python 脚本登陆邮箱 查找 指定邮件。

 

 

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/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

 

posted @   悟透  阅读(1268)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示