title: How to connect outlook IMAP server go through proxy server uisng perl
body: <p>I want to connect IMAP server but i am not able to connect directly imap server that's why i used proxy but still i am not able to connect and read emails.
  Following is my code,</p>
  
  <pre><code>#!/usr/intel/bin/perl
  use strict;
  use warnings;
  
  # fill in your details here
  my $username = 'username@companyname.com';
  my $password = 'password';
  my $mailhost = 'outlook.office365.com';#imap-mail.outlook.com
  my $mailport = 993;
  my $proxyhost = '121.244.253.5';
  my $proxyport = 8080;
  
  print "Proxy...\n";
  use IO::Socket::Socks::Wrapper(
                                   {
                                      ProxyAddr =&gt; $proxyhost,
                                      ProxyPort =&gt; $proxyport,
                                      SocksDebug =&gt; 0,
                                      Timeout =&gt; 100000000
                                   }
                                );
  
  # required modules
  use Net::IMAP::Simple;
  use Email::Simple;
  use IO::Socket::SSL;
  
  print "Connecting...\n";
  $IO::Socket::SSL::DEBUG=2;
  # Connect
  my $imap = Net::IMAP::Simple-&gt;new(
      $mailhost,
      port    =&gt; $mailport,
      use_ssl =&gt; 1
  ) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr \n";
  
  print "Logging In...\n";
  # Log in
  if ( !$imap-&gt;login( $username, $password ) ) {
      print STDERR "Login failed: " . $imap-&gt;errstr . "\n";
      exit(64);
  }
  
  print "Selecting Folder...\n";
  # Look in the the INBOX
  my $nm = $imap-&gt;select('Archive');
  
  print "How Many Messages Are There...\n";
  # How many messages are there?
  my ($unseen, $recent, $num_messages) = $imap-&gt;status();
  print "unseen: $unseen, recent: $recent, total: $num_messages\n\n";
  
  print "Quickly Look for unseen messages...\n";
  ## Iterate through unseen messages
  for ( my $i = 1 ; $i &lt;= $nm ; $i++ ) {
      if ( $imap-&gt;seen($i) ) {
          next;
      } else {
          my $es = Email::Simple-&gt;new( join '', @{ $imap-&gt;top($i) } );
  
          printf( "[%03d] %s\n\t%s\n", $i, $es-&gt;header('From'), $es-&gt;header(+'Subject') );
      }
  }
  
  print "Disconnect...\n";
  # Disconnect
  $imap-&gt;quit;
  
  print "Exit...\n";
  exit;
  </code></pre>
  
  <p>Following is my Response :- </p>
  
  <pre><code>Proxy...
  Connecting...
  DEBUG: .../IO/Socket/SSL.pm:332: socket not yet connected
  DEBUG: .../IO/Socket/SSL.pm:334: socket connected
  DEBUG: .../IO/Socket/SSL.pm:347: ssl handshake not started
  DEBUG: .../IO/Socket/SSL.pm:377: set socket to non-blocking to enforce timeout=100000000
  </code></pre>
  
  <p>I don't know why its not going further. Please share your opinion and correct me.</p>
  
  <p>Please help me here...</p>