美文网首页
读写邮件

读写邮件

作者: 黄yy家的jby | 来源:发表于2023-07-16 23:20 被阅读0次
    1. 准备
      pycharm中先把自身所需要的库更新完全
    pip install pipreqs
    pipreqs . --encoding=utf-8
    pip install -r requirements.txt
    
    1. 读邮件
    SMTP_SERVER = 'coremail.xxxx.com.cn'  # 更改为你的SMTP服务器
    
    def read_mail(folder = 'inbox', USERNAME = USERNAME, PASSWORD = PASSWORD):
        """
        这个函数用于读取指定收件箱的最近一封邮件
        """
        mail = imaplib.IMAP4_SSL(IMAP_SERVER)
        mail.login(USERNAME, PASSWORD)
        if folder != 'inbox':
            folder_utf7 = imapclient.imap_utf7.encode('量化部')
            folder = f"INBOX/{folder_utf7.decode('utf-8')}"
        mail.select(folder)
    
        result, data = mail.uid('search', None, "ALL")
        latest_email_uid = data[0].split()[-1]
        
        result, email_data = mail.uid('fetch', latest_email_uid, '(BODY.PEEK[HEADER.FIELDS (SUBJECT FROM)])')
        raw_email = email_data[0][1].decode("utf-8")
        email_message = email.message_from_string(raw_email)
    
        subject = decode_header(email_message['Subject'])[0][0]
        if isinstance(subject, bytes):
            # 如果是bytes类型,需要进行解码
            subject = subject.decode()
        from_ = decode_header(email_message['From'])[0][0]
        if isinstance(from_, bytes):
            from_ = from_.decode()
    
        print('From :', from_)
        print('Subject :', subject)
    

    3.写邮件

    https://blog.csdn.net/fenglepeng/article/details/107005000

    相关文章

      网友评论

          本文标题:读写邮件

          本文链接:https://www.haomeiwen.com/subject/ixwwudtx.html