开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 181|回复: 1
收起左侧

[已解决] 求一个python的微软邮箱取件源码

 关闭 [复制链接]
结帖率:85% (88/104)
发表于 2025-5-26 23:41:50 | 显示全部楼层 |阅读模式   河北省唐山市
9精币
求一个python的微软邮箱取件源码

最佳答案

查看完整内容

import email as email_reader import requests import imaplib from email.header import decode_header from bs4 import BeautifulSoup # 添加HTML解析库 def get_access_token_from_refresh_token(refresh_token, client_id): headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', } data = { "client_id": client_id, "refresh_token": refresh_token, ...

回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值无答案申请取消悬赏投诉有答案未采纳为最佳
结帖率:85% (46/54)

签到天数: 1 天

发表于 2025-5-26 23:41:51 | 显示全部楼层   山西省太原市
import email as email_reader
import requests
import imaplib
from email.header import decode_header
from bs4 import BeautifulSoup  # 添加HTML解析库


def get_access_token_from_refresh_token(refresh_token, client_id):
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    }
    data = {
        "client_id": client_id,
        "refresh_token": refresh_token,
        "grant_type": "refresh_token"
    }

    try:
        rr = requests.post(
            "https://login.microsoftonline.com/common/oauth2/v2.0/token",
            headers=headers,
            data=data,
            timeout=10
        )
        rr.raise_for_status()

        response_data = rr.json()
        if "error" not in response_data:
            return {
                "code": 0,
                "access_token": response_data["access_token"],
                "refresh_token": response_data.get("refresh_token", refresh_token)
            }
        elif "error_description" in response_data and "User account is found to be in service abuse mode" in \
                response_data["error_description"]:
            return {"code": 1, "message": "账户被封锁或凭证错误"}
        else:
            return {"code": 1, "message": f"获取访问令牌失败: {response_data.get('error_description', '未知错误')}"}

    except requests.exceptions.RequestException as e:
        return {"code": 1, "message": f"网络请求错误: {str(e)}"}


def imap_authenticate_with_oauth2(username, access_token):
    try:
        auth_string = f"user={username}\1auth=Bearer {access_token}\1\1"
        mail = imaplib.IMAP4_SSL("outlook.office365.com")
        mail.authenticate("XOAUTH2", lambda x: auth_string)
        return mail
    except Exception as e:
        raise Exception(f"IMAP认证失败: {str(e)}")


def decode_str(s):
    value, charset = decode_header(s)[0]
    if charset:
        value = value.decode(charset)
    return value


def html_to_text(html_content):
    """将HTML内容转换为纯文本"""
    if not html_content:
        return ""

    soup = BeautifulSoup(html_content, 'html.parser')
    # 处理pre标签,保留换行
    for pre in soup.find_all('pre'):
        pre.replace_with(f"\n{pre.get_text()}\n")

    # 处理br标签,转换为换行
    for br in soup.find_all('br'):
        br.replace_with("\n")

    # 获取纯文本并处理多余空白
    text = soup.get_text()
    # 合并多个空白为一个
    text = ' '.join(text.split())
    # 恢复换行
    text = text.replace(' \n ', '\n')
    text = text.replace('\n ', '\n')
    text = text.replace(' \n', '\n')

    return text


def get_latest_email(email, access_token):
    mail = imap_authenticate_with_oauth2(email, access_token)

    try:
        mail.select("inbox")
        status, messages = mail.search(None, 'ALL')
        if not messages[0]:
            return {"code": 1, "message": "收件箱为空"}

        latest_mail_id = messages[0].split()[-1]
        status, msg_data = mail.fetch(latest_mail_id, '(RFC822)')

        raw_email = msg_data[0][1]
        msg = email_reader.message_from_bytes(raw_email)

        subject = decode_str(msg["Subject"])

        content = ""
        content_type = ""

        if msg.is_multipart():
            # 优先获取text/plain部分
            for part in msg.walk():
                part_content_type = part.get_content_type()
                content_disposition = str(part.get("Content-Disposition"))

                try:
                    part_body = part.get_payload(decode=True)
                    if not part_body:
                        continue

                    # 尝试解码内容
                    charset = part.get_content_charset() or 'utf-8'
                    try:
                        decoded_body = part_body.decode(charset, errors='replace')
                    except LookupError:
                        decoded_body = part_body.decode('utf-8', errors='replace')

                    if part_content_type == "text/plain" and "attachment" not in content_disposition:
                        content = decoded_body
                        content_type = "text/plain"
                        break
                    elif part_content_type == "text/html" and "attachment" not in content_disposition and not content:
                        content = decoded_body
                        content_type = "text/html"
                except Exception as e:
                    continue
        else:
            body = msg.get_payload(decode=True)
            if body:
                charset = msg.get_content_charset() or 'utf-8'
                try:
                    content = body.decode(charset, errors='replace')
                except LookupError:
                    content = body.decode('utf-8', errors='replace')
                content_type = msg.get_content_type()

        # 如果是HTML内容,转换为纯文本
        if content_type == "text/html":
            content = html_to_text(content)

        return {
            "code": 0,
            "subject": subject,
            "content": content
        }
    except Exception as e:
        return {"code": 1, "message": f"读取邮件失败: {str(e)}"}
    finally:
        mail.close()
        mail.logout()


def example():
    email = '<EMAIL>'
    client_id = 'your_client_id'
    refresh_token = "your_refresh_token"

    token_result = get_access_token_from_refresh_token(refresh_token, client_id)
    if token_result["code"] != 0:
        print(f"获取令牌失败: {token_result['message']}")
        return

    email_result = get_latest_email(email, token_result["access_token"])
    if email_result["code"] == 0:
        print(f"主题: {email_result['subject']}")
        print(f"内容: {email_result['content'][:2000]}...")
    else:
        print(f"获取邮件失败: {email_result['message']}")


if __name__ == '__main__':
    example()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表