无法连接到outlook.office365.com以使用Perl Module Mail :: IMAPClient处理电子邮件

时间:2016-12-16 13:00:10

标签: perl outlook office365 imap mail-server

我已经搜索了几天,但最终用完了选项,我的一位客户将其电子邮件帐户移至" outlook.office365.com" ,现在我的所有perl脚本都失败了因为他们的平台上有一组不同的配置,所以我不得不重写我的所有perl脚本以使一切工作再次起作用,我可以用模块发送电子邮件" Email :: Sender ::传输:: SMTP :: TLS" 但我没有使用我的旧模块来阅读和处理电子邮件,例如" Mail :: POP3Client"

这是尝试的代码:

use Mail::IMAPClient;
use Email::MIME;

my $username = 'user';
my $password = 'pass';
my $server = 'outlook.office365.com';


print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new(
    Server => $server,
    Username => $username,
    Password => $password,
    Port => 993,
    Ssl => 1,
    Authmechanism => "PLAIN",
    Debug => 1,
)or die "Cannot connect to $mailhost as $username: $@";

print "upgrading connection to TLS \n";
$imap->starttls
(
    SSL_verify_mode => 0,
) or die "starttls failed: $@\n";

$imap->User($username);
$imap->Password($password);

print "Logging In\n";
$imap->login() or die "imap login failed: $@\n";

这是我在stackoverflow上找到的解决方案,但它对我不起作用,我检查了防火墙配置,一切正常。

这是输出:

Anon connect to IMAP
Started at Fri Dec 16 13:54:25 2016
Using Mail::IMAPClient version 3.38 on perl 5.022001
Connecting with IO::Socket::SSL PeerAddr outlook.office365.com PeerPort 993 Proto tcp Timeout 600 Debug 1
ERROR: Unable to connect to outlook.office365.com:  at C:/Perl64/site/lib/Mail/IMAPClient.pm line 370.
        Mail::IMAPClient::connect(Mail::IMAPClient=HASH(0xcac170)) called at C:/Perl64/site/lib/Mail/IMAPClient.pm line 314
        Mail::IMAPClient::new("Mail::IMAPClient", "Server", "outlook.office365.com", "Username", "user", "Password", "pass", "Port", 993, ...) called at dont.pl line 10
Cannot connect to  as user: Unable to connect to outlook.office365.com:  at dont.pl line 10.

检查我的SSL连接:

openssl s_client -connect outlook.office365.com:993

结果:

* OK The Microsoft Exchange IMAP4 service is ready. 

所以有什么想法吗?这段代码有什么问题?

1 个答案:

答案 0 :(得分:0)

编辑:感谢Max,最后一个解决方案:删除startls可以进入登录步骤。

use Mail::IMAPClient;
use Email::MIME;

my $username = 'user';
my $password = 'pass';
my $server = 'outlook.office365.com';


print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new(
    Server => $server,
    Port => 993,
    Ssl => 1,
    Authmechanism => "PLAIN",
    Debug => 1,
)or die "Cannot connect to $mailhost as $username: $@";

$imap->User($username);
$imap->Password($password);

print "Logging In\n";
$imap->login() or die "imap login failed: $@\n";

在我的机器上,它输出:

Anon connect to IMAP
Started at Fri Dec 16 16:59:31 2016
Using Mail::IMAPClient version 3.38 on perl 5.022001
Read:   * OK The Microsoft Exchange IMAP4 service is ready. [RABCADYAUABSADAAMgAwADEAQwBBADAAMAAyADcALgBlAHUAcgBwAHIAZAAwADIALgBwAHIAbwBkAC4AbwB1AHQAbABvAG8AawAuAGMAbwBtAA==]
upgrading connection to TLS 
Sending: 1 CAPABILITY
Sent 14 bytes
Read:   * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
        1 OK CAPABILITY completed.
Logging In
Sending: 2 AUTHENTICATE PLAIN
Sent 22 bytes
Read:   +
Sending: [Redact: Count=2 Showcredentials=OFF]
Sent 18 bytes
Read:   2 NO AUTHENTICATE failed.
ERROR: 2 NO AUTHENTICATE failed. at /usr/local/share/perl/5.22.1/Mail/IMAPClient.pm line 3261.
        Mail::IMAPClient::authenticate(Mail::IMAPClient=HASH(0x14f6a30), "PLAIN", undef) called at /usr/local/share/perl/5.22.1/Mail/IMAPClient.pm line 562
        Mail::IMAPClient::login(Mail::IMAPClient=HASH(0x14f6a30)) called at imapt.pl line 30
imap login failed: 2 NO AUTHENTICATE failed.

这是正常的,因为我没有有效的帐户。