我整天都在与 IMAP 和 office365 搏斗,但没有成功。
据我所知,登录失败,尽管首先建立 imap 连接,然后使用$imap->starttls请求将连接升级到 TLS 。
我在 windows 7 64bit 上运行草莓 perl 5.20。
代码:
#!/usr/bin/perl
use strict;
use warnings;
$|=1;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
use Data::Dumper;
use Mail::IMAPClient;
my $host = 'outlook.office365.com';
my $id = 'XXX';
my $pass = 'XXX';
print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new
(
Server => $host,
# Username => $id,
# Password => $pass,
Debug => 1,
)
|| die "Failed to connect to IMAP server\n";
print "Upgrade connection to TLS\n";
$imap->starttls
(
SSL_verify_mode => 0,
) or die "starttls failed: $@\n";
print "Logging In\n";
$imap->login($id, $pass);
print "Getting Folder\n";
my $folder = $imap->Folder();
print "Folder = $folder\n";
print "Listing Folders\n";
foreach my $f ( grep($imap->selectable($_),$imap->folders ) )
{
print "The $f folder has ",
$imap->unseen_count($f)||0,
" unseen messages.\n";
}
exit;
print "Listing Folders\n";
foreach my $f ($imap->folders)
{
print "The $f folder has ",
$imap->unseen_count($f)||0, " unseen messages.\n";
}
输出:
perl imap.pl
Anon connect to IMAP
Started at Thu Jun 18 20:15:08 2015
Using Mail::IMAPClient version 3.35 on perl 5.020002
Connecting with IO::Socket::INET PeerAddr outlook.office365.com PeerPort 143 Proto tcp Timeout 600 Debug 1
Connected to outlook.office365.com errno(A connect request was made on an already connected socket.)
not using Fast_IO; not available on this platform
Read: * OK The Microsoft Exchange IMAP4 service is ready. [SABLAFgAUABSADAAMgBDAEEAMAAwADUAMQAuAGEAcABjAHAAc
gBkADAAMgAuAHAAcgBvAGQALgBvAHUAdABsAG8AbwBrAC4AYwBvAG0A]
Upgrade connection to TLS
Sending: 1 STARTTLS
Sent 12 bytes
Read: 1 OK Begin TLS negotiation now.
Logging In
Getting Folder
Use of uninitialized value $folder in concatenation (.) or string at imap2.pl line 37.
Folder =
Listing Folders
Sending: 2 LIST "" *
Sent 13 bytes
Read: 2 BAD Command received in Invalid state.
ERROR: 2 BAD Command received in Invalid state. at C:/Strawberry/perl/site/lib/Mail/IMAPClient.pm line 1353.
Mail::IMAPClient::__ANON__("2 BAD Command received in Invalid state.\x{d}\x{a}") called at C:/Strawber
ry/perl/site/lib/Mail/IMAPClient.pm line 1389
Mail::IMAPClient::_get_response(Mail::IMAPClient=HASH(0x87df88), 2, undef) called at C:/Strawberry/per
l/site/lib/Mail/IMAPClient.pm line 1315
Mail::IMAPClient::_imap_command_do(Mail::IMAPClient=HASH(0x87df88), "LIST \"\" *") called at C:/Strawb
erry/perl/site/lib/Mail/IMAPClient.pm line 1214
Mail::IMAPClient::_imap_command(Mail::IMAPClient=HASH(0x87df88), "LIST \"\" *") called at C:/Strawberr
y/perl/site/lib/Mail/IMAPClient.pm line 652
Mail::IMAPClient::_list_or_lsub(Mail::IMAPClient=HASH(0x87df88), "LIST", undef, undef) called at C:/St
rawberry/perl/site/lib/Mail/IMAPClient.pm line 658
Mail::IMAPClient::list(Mail::IMAPClient=HASH(0x87df88), undef, undef) called at C:/Strawberry/perl/sit
e/lib/Mail/IMAPClient.pm line 696
Mail::IMAPClient::_folders_or_subscribed(Mail::IMAPClient=HASH(0x87df88), "list", undef) called at C:/
Strawberry/perl/site/lib/Mail/IMAPClient.pm line 716
Mail::IMAPClient::folders(Mail::IMAPClient=HASH(0x87df88)) called at imap2.pl line 40
ERROR: 2 BAD Command received in Invalid state. at C:/Strawberry/perl/site/lib/Mail/IMAPClient.pm line 1263.
Mail::IMAPClient::_imap_command(Mail::IMAPClient=HASH(0x87df88), "LIST \"\" *") called at C:/Strawberr
y/perl/site/lib/Mail/IMAPClient.pm line 652
Mail::IMAPClient::_list_or_lsub(Mail::IMAPClient=HASH(0x87df88), "LIST", undef, undef) called at C:/St
rawberry/perl/site/lib/Mail/IMAPClient.pm line 658
Mail::IMAPClient::list(Mail::IMAPClient=HASH(0x87df88), undef, undef) called at C:/Strawberry/perl/sit
e/lib/Mail/IMAPClient.pm line 696
Mail::IMAPClient::_folders_or_subscribed(Mail::IMAPClient=HASH(0x87df88), "list", undef) called at C:/
Strawberry/perl/site/lib/Mail/IMAPClient.pm line 716
Mail::IMAPClient::folders(Mail::IMAPClient=HASH(0x87df88)) called at imap2.pl line 40