开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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


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

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

查看: 2360|回复: 198
收起左侧

[其它源码] 微软OAuth2 IMAP收件

  [复制链接]
结帖率:100% (10/10)
发表于 2025-5-14 18:15:09 | 显示全部楼层 |阅读模式   福建省泉州市
分享源码
界面截图: -
是否带模块: 调用了模块
备注说明: -
游客,如果您要查看本帖隐藏内容请回复

评分

参与人数 22好评 +1 精币 +21 收起 理由
洛哥 + 1 感谢分享,很给力!~
YunXiGe + 1 感谢分享,很给力!~
jc520hll + 1 感谢分享,很给力!~
z134 + 1 感谢分享,很给力!~
ma4747 + 1 感谢分享,很给力!~
cbl521ysys + 1 感谢分享,很给力!~
无尘666 + 1 感谢分享,很给力!~
3266167 + 1 感谢分享,很给力!~
huangdi9569 + 1 感谢分享,很给力!~
zjbin1989 + 1 感谢分享,很给力!~
liy456 + 1 感谢分享,很给力!~
文西哥 + 1 感谢分享,很给力!~
望尘莫及 + 1 感谢分享,很给力!~
暮光之城 + 1 感谢分享,很给力!~
初阳123 + 1 感谢分享,很给力!~
hackjoe + 1 感谢分享,很给力!~
风雨3137 + 1 感谢分享,很给力!~
sundanming + 1 感谢分享,很给力!~
pcghr355 + 1 感谢分享,很给力!~
huangdi956 + 1 感谢分享,很给力!~
Zźh926 + 1 感谢分享,很给力!~
多多帅吧 + 1 共同努力,共同进步

查看全部评分


签到天数: 3 天

发表于 1 小时前 | 显示全部楼层   广东省深圳市
谢谢分享
回复 支持 反对

使用道具 举报

结帖率:0% (0/7)

签到天数: 1 天

发表于 昨天 21:17 | 显示全部楼层   湖北省黄冈市
感谢楼主的分享,学习一下
回复 支持 反对

使用道具 举报

签到天数: 8 天

发表于 昨天 17:46 | 显示全部楼层   波兰

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 = ''
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()

回复 支持 反对

使用道具 举报

签到天数: 8 天

发表于 昨天 17:44 | 显示全部楼层   波兰
感谢分享,很给力!~
回复 支持 反对

使用道具 举报

结帖率:88% (7/8)

签到天数: 1 天

发表于 昨天 17:11 | 显示全部楼层   福建省福州市
牛逼牛逼牛逼牛逼牛逼牛逼
回复 支持 反对

使用道具 举报

结帖率:80% (4/5)

签到天数: 2 天

发表于 昨天 17:08 | 显示全部楼层   福建省三明市
        感谢分享,很给力!~
回复 支持 反对

使用道具 举报

结帖率:100% (2/2)

签到天数: 2 天

发表于 昨天 16:05 | 显示全部楼层   江苏省连云港市
太强了吧必须需要
回复 支持 反对

使用道具 举报

结帖率:96% (22/23)

签到天数: 13 天

发表于 昨天 14:31 | 显示全部楼层   山东省济南市
谢谢分享!!!
回复 支持 反对

使用道具 举报

签到天数: 2 天

发表于 昨天 14:07 | 显示全部楼层   河南省许昌市
感谢分享,很给力!~
回复 支持 反对

使用道具 举报

结帖率:67% (2/3)

签到天数: 13 天

发表于 昨天 14:00 | 显示全部楼层   湖南省长沙市
1111111111111
回复 支持 反对

使用道具 举报

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

本版积分规则 致发广告者

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

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

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