【问题标题】:How to connect outlook IMAP server go through proxy server uisng perl如何使用 perl 通过代理服务器连接 Outlook IMAP 服务器
【发布时间】:2018-01-23 09:30:35
【问题描述】:

我想连接 IMAP 服务器,但我无法直接连接 imap 服务器,这就是我使用代理但仍然无法连接和阅读电子邮件的原因。 以下是我的代码,

#!/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 => $proxyhost,
                                    ProxyPort => $proxyport,
                                    SocksDebug => 0,
                                    Timeout => 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->new(
    $mailhost,
    port    => $mailport,
    use_ssl => 1
) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr \n";

print "Logging In...\n";
# Log in
if ( !$imap->login( $username, $password ) ) {
    print STDERR "Login failed: " . $imap->errstr . "\n";
    exit(64);
}

print "Selecting Folder...\n";
# Look in the the INBOX
my $nm = $imap->select('Archive');

print "How Many Messages Are There...\n";
# How many messages are there?
my ($unseen, $recent, $num_messages) = $imap->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 <= $nm ; $i++ ) {
    if ( $imap->seen($i) ) {
        next;
    } else {
        my $es = Email::Simple->new( join '', @{ $imap->top($i) } );

        printf( "[%03d] %s\n\t%s\n", $i, $es->header('From'), $es->header(+'Subject') );
    }
}

print "Disconnect...\n";
# Disconnect
$imap->quit;

print "Exit...\n";
exit;

以下是我的回应:-

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

我不知道为什么它不走得更远。请分享您的意见并纠正我。

请帮帮我...

【问题讨论】:

  • 请不要编辑您的问题以在答案或 cmets 中包含建议。
  • 好的。请帮我。非常紧急。
  • 看起来您使用的是旧版本的 IO::Socket::SSL。鉴于您在调试输出中显示的行号,版本最多为 1.38,即 7 年前的版本。我不确定您系统上的其他模块有多旧,但您的软件堆栈的年龄可能是您遇到的问题的一部分。另一个问题可能是您使用的代理:端口 8080 是 HTTP 代理的常用端口,而不是 SOCKS 代理,并且 SOCKS 包装器不会让您通过 HTTP 代理。

标签: perl sockets ssl office365 perl-module


【解决方案1】:

正确的语法是:

my $proxyhost = '65.130.4.202';
my $proxyport = 24451;
use Net::IMAP::Simple;

print "Proxy...\n";
use IO::Socket::Socks::Wrapper(
                              Net::IMAP::Simple => {
                                    ProxyAddr => $proxyhost,
                                    ProxyPort => $proxyport,
                                    SocksDebug => 3,
                                    Timeout => 100000000
                                 }
                              );


use IO::Socket::SSL;

print "Connecting...\n";
$IO::Socket::SSL::DEBUG=2;
# Connect
my $imap = Net::IMAP::Simple->new(
    'imap.gmail.com',
    port    => 993,
    use_ssl => 1
) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr \n";

print "Logging In...\n";

# Log in
if ( !$imap->login( 'pappucant@gmail.com', 'pappu123' ) ) {
    print STDERR "Login failed: " . $imap->errstr . "\n";
    exit(64);
}

对我来说工作得很好,没有与代码中提到的代理服务器一起工作。

输出:

Proxy...
Connecting...
DEBUG: .../IO/Socket/SSL.pm:625: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:627: socket connected
DEBUG: .../IO/Socket/SSL.pm:649: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:682: using SNI with hostname imap.gmail.com
DEBUG: .../IO/Socket/SSL.pm:738: set socket to non-blocking to enforce timeout=90
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:809: ssl handshake done
Logging In...
Login failed: [AUTHENTICATIONFAILED] Invalid credentials (Failure)

【讨论】:

  • 错误:-代理...连接...调试:.../IO/Socket/SSL.pm:332:套接字尚未连接调试:.../IO/Socket/SSL .pm:1203: SSL object not openerror:00000000:lib(0):func(0):reason(0) DEBUG: .../IO/Socket/SSL.pm:1203: IO::Socket::INET 配置failederror:00000000:lib(0):func(0):reason(0) DEBUG: .../IO/Socket/SSL.pm:332: 套接字尚未连接
  • @Thribhuwan:您是如何检查代理是否正常工作的?具体来说,您是如何检查代理是否作为 SOCKS 代理而不是 HTTP 代理工作的?
  • @Steffen 我对 linux 服务器了解不多。这只是 http 代理。请建议我通过 perl 中的 http 代理连接 imap 服务器的更好选择。
  • @Thribhuwan:如果这是一个 HTTP 代理,那么它不能用于隧道 IMAP,因为 IMAP(邮件)是不同于 HTTP(Web)的协议。您可能会尝试按照sandalov.org/blog/2088 中的说明通过此代理构建隧道,但不清楚代理配置是否允许这样做。
  • @Steffen 我也有同样的想法,但客户端不允许在 Unix 机器上安装代理隧道。我正在访问邮件服务器,但我收到空白回复。我将向客户端解释 IMAP 不允许使用隧道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2014-06-03
  • 2010-10-05
相关资源
最近更新 更多