zoukankan      html  css  js  c++  java
  • python实现收邮件判断模块poplib,email

    一、代码

    # 输入邮件地址, 口令和POP3服务器地址:
    import datetime
    import email
    import poplib
    import email.policy
    from email.parser import Parser
    flag=False
    
    emailaddress = '******'
    # 注意使用开通POP,SMTP等的授权码
    password = '*******'
    pop3_server = '*******'
    
    # 连接到POP3服务器:
    server = poplib.POP3(pop3_server)
    # 可以打开或关闭调试信息:
    # server.set_debuglevel(1)
    # POP3服务器的欢迎文字:
    print(server.getwelcome())
    
    # 身份认证:
    server.user(self.emailaddress)
    server.pass_(self.password)
    
    # stat()返回邮件数量和占用空间:
    messagesCount, messagesSize = server.stat()
    print('messagesCount:', messagesCount)
    print('messagesSize:', messagesSize)
    # list()返回所有邮件的编号:
    resp, mails, octets = server.list()
    print("resp:", resp)
    print("mails:", mails)
    print("octets:", octets)
    
    length = len(mails)
    
    print(length)
    
    for i in range(length):
        i=length-i
        resp, lines, octets = server.retr(i)
        msg_content=b'
    '.join(lines)
        msg = email.parser.BytesParser(policy=email.policy.default).parsebytes(msg_content)
        print('发件人', msg['from'])
        print('收件人', msg['to'])
        print('主题', msg['subject'])
        print('第一个收件人用户名', msg['to'].addresses[0].username)
        print('第一个发件人用户名', msg['from'].addresses[0].username)
        theme= msg['subject']
        email_content=[part.get_content() for part in msg.walk() if part.get_content_maintype() == 'text'][0]
        if theme=="启动查库任务分派机器人" and email_content==datetime.datetime.now().strftime("%Y%m%d"):
            flag=True
            break
    
    server.quit()
    print(flag)

    二、解决编码问题

    import chardet
    
    #继承重写解析类
    class BytesParser_new(email.parser.BytesParser):
        def parsebytes(self, text, headersonly=False):
            import chardet
            encoding=chardet.detect(text).get("encoding")
            text = text.decode(encoding, errors='surrogateescape')
            return self.parser.parsestr(text, headersonly)
    
    msg = BytesParser_new(policy=email.policy.default).parsebytes(msg_content)

    三、代码封装

    import email
    import poplib
    import email.policy
    
    
    class ReadEmail(object):
        def __init__(self, emailaddress, password, pop3_server):
            self.emailaddress = emailaddress
            self.password = password
            self.pop3_server = pop3_server
            self.server = poplib.POP3(pop3_server)
    
        def login(self):
            # 可以打开或关闭调试信息:
            self.server.set_debuglevel(1)
            # POP3服务器的欢迎文字:
            print(self.server.getwelcome())
            # 身份认证:
            self.server.user(emailaddress)
            self.server.pass_(password)
    
        def read(self):
    self.login()
    # stat()返回邮件数量和占用空间: messagesCount, messagesSize = self.server.stat() print('messagesCount:', messagesCount) print('messagesSize:', messagesSize) # list()返回所有邮件的编号: resp, mails, octets = self.server.list() print("resp:", resp) print("mails:", mails) print("octets:", octets) length = len(mails) print(length) for i in range(1, length): resp, lines, octets = self.server.retr(i) msg_content = b' '.join(lines) msg = email.parser.BytesParser(policy=email.policy.default).parsebytes(msg_content) # print('发件人', msg['from']) # print('收件人', msg['to']) print('主题', msg['subject']) # print('第一个收件人用户名', msg['to'].addresses[0].username) # print('第一个发件人用户名', msg['from'].addresses[0].username) theme = msg['subject'] email_content = [part.get_content() for part in msg.walk() if part.get_content_maintype() == 'text'][0] if theme == "系统退信/The email is returned": self.server.dele(i) print("删除邮件成功:", msg['subject']) self.server.quit() if __name__ == '__main__': emailaddress = '********' # 注意使用开通POP,SMTP等的授权码 password = '********' pop3_server = 'imap.qq.com' rm=ReadEmail(emailaddress,password ,pop3_server ) rm.read()
  • 相关阅读:
    [angularjs] angularjs系列笔记(五)Service
    [android] 隐式意图的配置
    [android] 隐式意图激活另外一个activity
    [angularjs] angularjs系列笔记(四)过滤器
    [android] 显示意图激活另外一个activity
    [android] smartimageview&常见的开源代码
    [angularjs] angularjs系列笔记(四)控制器
    [android] 上传文件到服务器
    [android] 异步http框架与实现原理
    [android] 采用httpclient提交数据到服务器
  • 原文地址:https://www.cnblogs.com/angelyan/p/12096131.html
Copyright © 2011-2022 走看看