Perl office365 imap TLS access not working using IMAPClient

I've been struggling with IMAP and office365 all day with no success.

Beside how can I tell the login fails even though I made an imap connection first and then requested a TLS connection with $ imap-> starttls .

I am running strawberry perl 5.20 on windows 7 64bit.

CODE:

#!/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";
}

      

OUTPUT:

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

      

+3


source to share


2 answers


This code should work:



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";

      

+1


source


Id +1 gangabass' answer if I could (thanks to helper) Here is a more helpful answer for people (like me) stuck inside a corporate environment with annoying firewall rules (i.e. Port 993 is blocked):



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";

      

0


source







All Articles