针对个人免费版 Outlook (Outlook.com / Hotmail),要在 Cloudflare Workers 中使用,最正规且免费的方式是使用 Microsoft Graph API。
由于个人版 Outlook 不支持简单的“API Key”模式,也不支持“客户端凭证”模式(Client Credentials,即只用 ID 和 Secret 就能跑),必须使用 “OAuth2 授权码模式”。
第一步:注册应用(Step 1: Register an Application)
这是获取 Client ID 的步骤。
- 登录 Azure 门户 / Login to Azure Portal: https://portal.azure.com https://entra.microsoft.com
- 搜索并进入“应用注册”/ Search for and select "App registrations".
- 点击“新注册”/ Click "New registration" in the top menu.
- 填写注册信息 (Fill in the details):
- 名称 (Name): 输入一个名字,例如 MyEmailBot。
- 受支持的账户类型 (Supported account types): 关键! 选择第三项 “任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户和个人 Microsoft 帐户(例如,Skype、Xbox)”。这是最通用的选项,支持个人 Outlook/Hotmail 邮箱。/ "Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)".
- 重定向 URI (Redirect URI):
- 平台 (Platform): 选择 Web。
- URL: 填入 http://localhost (如果您只是为了手动获取 Token) 或您的回调地址。
- 点击“注册”/ "Register"
- 保存 Application ID
- 注册成功后,复制 Application (client) ID,稍后会用到。
第二步:创建客户端密钥(Step 2: Create a Client Secret)
这是获取 Client Secret 的步骤。
- 点击左侧菜单的“证书和密码”/ "Certificates & secrets"
- 点击“新客户端密码”
- 添加说明和过期时间
- 说明 (Description): 随便填,如 login_key。
- 过期时间 (Expires): 建议选择最长时间(如 24个月或推荐值)。
- 点击“添加”/ "Add"
- 立即复制“值” (Value)
- 重要: 请立即复制 Value 这一列的内容。刷新页面后它将不再显示。这就是您的 client_secret。
第三步:配置 API 权限(Step 3: Configure API Permissions)
这是您之前询问的核心步骤,用于授权应用读写邮件。 This is the core step to authorize the app to read and send emails.
- 点击左侧菜单的“API 权限”/ "API permissions"
- 点击“添加权限”/ "+ Add a permission"
- 选择“Microsoft Graph”
- 选择“委托的权限”/ "Delegated permissions"
- 搜索并勾选以下权限 (Search and check the following permissions):
- User.Read (通常默认已选 / Usually selected by default)
- Mail.Read (读取邮件 / Read user mail)
- Mail.Send (发送邮件 / Send mail as a user)
- offline_access (非常重要:用于获取长期有效的 Refresh Token / Crucial: For getting a persistent Refresh Token)
- 点击底部的“添加权限”/ Add permissions按钮
- 这是您之前问的那一步: 在选择完所有权限后,必须点击面板底部的蓝色 "Add permissions" 按钮来保存更改。
- 最后一定点代表 Default Directory 授予管理员同意
第四步:获取 Refresh Token (Step 4: Get Refresh Token)
这是一次性操作,用于获取“钥匙”/ key。
- 在浏览器地址栏拼接并访问以下 URL (Replace [YOUR_CLIENT_ID]):
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=您的ClientID&response_type=code&redirect_uri=http://localhost&response_mode=query&scope=offline_access%20https://graph.microsoft.com/Mail.Read%20https://graph.microsoft.com/Mail.Send&prompt=consent - 登录并同意授权 / "Accept" (同意)
- 获取 Code
- 授权后浏览器会跳回 http://localhost/?code=M.R3_BAY...。
- 复制地址栏中 code= 后面的长字符串。
- 交换 Refresh Token (使用 Postman 或终端)
- 发送 POST 请求到。
https://login.microsoftonline.com/common/oauth2/v2.0/token - Body 参数 (x-www-form-urlencoded):
- client_id:[Your Client ID]
- client_secret:[Your Secret Value]
- code:[The code you copied]
- redirect_uri:http://localhost
- grant_type:authorization_code
- 结果 (Result):响应中会包含 refresh_token。请保存好它,以后您的代码将使用它来生成访问令牌。
$url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
$body = @{
client_id = ""# <--- 请在这里粘贴 应用程序(客户端) ID / Application (client) ID
# 👇 这里的 scope 必须和浏览器链接里的一模一样,不能多也不能少
scope = "offline_access https://graph.microsoft.com/Mail.Read https://graph.microsoft.com/Mail.Send"
code = "" # <--- 请在这里粘贴刚才获取的新 Code(M.C开头)
redirect_uri = "http://localhost"
grant_type = "authorization_code"
client_secret = "" # <--- 请在这里粘贴client_secret 新客户端密码的“值” (Value)
}
$response = Invoke-RestMethod -Method Post -Uri $url -Body $body
if ($response.refresh_token) {
Write-Host "✅ 成功!您的新 Refresh Token 是:" -ForegroundColor Green
Write-Host $response.refresh_token -ForegroundColor Yellow
} else {
Write-Host "❌ 失败,没有获取到 Token,请检查上方红字错误。" -ForegroundColor Red
}
总结
- Azure 注册应用(选个人版)。
- 浏览器授权一次,拿到 Code。
- API 权限。
- POST 请求换取 refresh_token(永久有效)。
这样配置后,您就拥有了一个完全免费、极其稳定(微软官方 API)且不会被封 IP 的收、发信渠道了。

发表评论
评论列表