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

Perl通过Net::POP3连接outlook.office365.com失败求助

解决方案

1. 切换到OAuth2认证(推荐)

Net::POP3原生不支持OAuth2,需使用支持该认证方式的模块,比如Net::POP3::OAuth2,步骤如下:

步骤1:安装依赖模块

cpanm Net::POP3::OAuth2 Authen::OAuth2

步骤2:获取OAuth2访问令牌

需要先在Azure Active Directory中注册应用:

  • 登录Azure门户,创建新的应用注册
  • 配置API权限:添加POP.AccessAsUser.All(Delegated权限)
  • 获取客户端ID、客户端密钥、租户ID
  • 通过授权码流程或客户端凭证流程获取访问令牌

步骤3:示例代码

use Net::POP3::OAuth2;

# 替换为你的实际信息
my $email = 'your_account@outlook.com';
my $access_token = 'YOUR_OAUTH2_ACCESS_TOKEN';

my $pop = Net::POP3::OAuth2->new(
    host     => 'outlook.office365.com',
    port     => 995,
    ssl      => 1,
    user     => $email,
    access_token => $access_token,
);

if ($pop->login) {
    my $total_msgs = $pop->count;
    print "Inbox has $total_msgs messages\n";
    
    # 获取最后一封邮件示例
    if ($total_msgs > 0) {
        my $last_msg = $pop->get($total_msgs);
        print @$last_msg;
    }
    
    $pop->quit;
} else {
    die "Login failed: " . $pop->message;
}

2. 替代方案:使用Exchange Web Services (EWS)

如果是Exchange Online账号,可使用Exchange::WebService模块直接通过EWS访问邮件,避免依赖POP3协议:

安装模块

cpanm Exchange::WebService

示例代码(使用应用密码,仅临时过渡)

use Exchange::WebService;

my $ews = Exchange::WebService->new(
    server   => 'outlook.office365.com',
    username => 'your_account@outlook.com',
    password => 'YOUR_APP_PASSWORD', # 需开启MFA后创建应用密码
);

# 获取收件箱最新10封邮件
my $items = $ews->find_items(
    folder   => 'Inbox',
    page_size => 10,
);

foreach my $item (@$items) {
    print "Subject: " . $item->Subject . "\n";
}

3. 临时过渡方案:应用密码(不推荐长期使用)

若账号开启了多因素认证(MFA),可创建应用密码替代原密码进行基本认证:

  1. 登录微软账户安全中心
  2. 进入「高级安全选项」→「应用密码」
  3. 创建新的应用密码,在Net::POP3代码中使用该密码登录

注意:微软计划逐步淘汰应用密码,此方案仅作为临时过渡。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 20:57:35