3

我整天都在与 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
4

2 回答 2

1

此代码必须有效:

my $imap = Mail::IMAPClient->new(
    Server            => $host,
    User              => $id,
    Password         => $pass,
    Port              => 993,
    Ssl                => 1,
    Authmechanism  => "PLAIN",
    Debug => 1
) or die "Cannot connect through IMAPClient: $@\n";
于 2015-06-18T17:11:19.560 回答
0

如果可以的话,ID +1 gangabass 的回答(感谢伙伴)对于那些被困在公司环境中且具有烦人的防火墙规则(即端口 993 被阻止)的人(比如我自己)来说,这是一个更有用的答案:

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

print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new(
    Server => $mailhost,
)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";
于 2015-06-18T17:53:01.147 回答