Unable to connect to outlook.office365.com for processing emails with Perl Module Mail::IMAPClient

3.8k views Asked by At

I have searched for days but finally run out of options, one of my clients move its emails accounts to "outlook.office365.com", now all my perl scripts fails because they have a different set of configurations on their platform, so i had to rewrite all of my perl scripts to make everything work again, i was aible to send emails with the module "Email::Sender::Transport::SMTP::TLS" but i didnt had the same luck using my old modules to read and proccess emails such as "Mail::POP3Client".

This is the code a try:

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

This is a solution that i have found on stackoverflow but it doesnt work for me, i checked my firewall configuration and everything is ok.

This is the output:

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.

To check my SSL connection:

openssl s_client -connect outlook.office365.com:993

Results:

* OK The Microsoft Exchange IMAP4 service is ready. 

So any ideas?, what is wrong with this code?

1

There are 1 answers

10
David Verdin On

EDIT: thanks to Max, finally a solution: removing startls allows to reach the login step.

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

On my machine, it outputs this:

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.

Which is normal, as I don't have a valid account.