You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chilkat IMAP无法登录Office365 技术求助

问题

我的Office365邮件检查IMAP代码去年秋季已失效(客户近期才发现),代码在登录环节失败。已确认账户IMAP仍启用,密码也正确。使用的是ChilkatDnCore31 v9.5.0.93,部署在Azure Functions实例中。

代码片段

Chilkat.Global glob = new Chilkat.Global();
var success = glob.UnlockBundle("MyUnlockCode");

if (success != true)
{
    log.LogError("ProcessLatestEmails. Error unlocking component: " + imap.LastErrorText);
    return false;
}

// To use a secure TLS/SSL connection, set the Ssl property and the port:
imap.Ssl = UseSSL;
imap.Port = Port;
success = imap.Connect(Server);

if (success != true)
{
    log.LogError("ProcessLatestEmails. Error connecting to server: " + imap.LastErrorText);
    return false;
}

success = imap.Login(UserName, Password);

if (success != true)
{
    log.LogError("ProcessLatestEmails. Error logging in: " + imap.LastErrorText);
    imap.Disconnect();
    imap.Dispose();
    return false;
}

错误日志

ProcessLatestEmails. Error logging in: ChilkatLog:

Login:

DllDate: Dec 29 2022

ChilkatVersion: 9.5.0.93

UnlockPrefix: UnlockCodeWasHere

Architecture: Little Endian; 64-bit

VerboseLogging: 0

imapServer: outlook.office365.com

loginX(2797ms):

login: adam@clientdomain.com

greeting: * OK The Microsoft Exchange IMAP4 service is ready. [QgBZAEEAUABSADIAMgBvAGQALgBv...AHUAdABsAG8AbwBrAC4AYwBvAG0A]

authenticateLogin(2797ms):

loginImap(2797ms):

ConnectionType: SSL/TLS

ImapCmdSent: aaab LOGIN "adam@clientdomain.com"

getCompleteResponse(2797ms):

ImapCmdResp: aaab NO LOGIN failed.

--getCompleteResponse

--loginImap

isOK: serverResponse: aaab NO LOGIN failed.

--isOK

--authenticateLogin

--loginX

Failed.

--Login

--ChilkatLog

请问这是已弃用的登录方式吗?是否需要改用OAuth认证,还是可以修复现有方式使其正常工作?


回答

原因分析

你遇到的问题是因为微软在2023年秋季正式禁用了Office 365的基本认证(包括IMAP的LOGIN明文密码登录方式),这是微软强制推行的安全政策,并非你的代码或账户配置问题,基本认证已无法恢复使用。

解决方案

必须改用OAuth 2.0认证,你的Chilkat版本(v9.5.0.93)支持Office 365的OAuth认证(从v9.5.0.85版本开始支持),调整步骤如下:

  1. 在Azure AD注册应用:
    • 注册新的应用程序,添加Microsoft Graph下的IMAP.AccessAsUser.All委托权限,并获取管理员同意。
    • 根据业务场景选择合适的OAuth流程:授权码流程适用于需要用户交互的场景,客户端凭据流程适用于服务端无交互的后台场景。
  2. 修改代码逻辑:
    • 先通过Azure AD获取有效的OAuth 2.0访问令牌。
    • 连接IMAP服务器后,不再调用Login方法,而是设置imap.OAuth2AccessToken为获取到的令牌,再调用imap.AuthenticateOAuth2()完成认证。

核心代码示例:

// 连接服务器成功后执行以下认证逻辑
imap.OAuth2AccessToken = "你的OAuth访问令牌";
success = imap.AuthenticateOAuth2();
if (!success)
{
    log.LogError("OAuth认证失败: " + imap.LastErrorText);
    imap.Disconnect();
    imap.Dispose();
    return false;
}

注意事项

  • Azure Functions环境中,需确保应用注册的权限配置正确,令牌获取逻辑能正常访问Azure AD服务。
  • 需实现令牌刷新机制,避免令牌过期导致认证失败。

内容的提问来源于stack exchange,提问作者Brad Mathews

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 02:20:28