2012-02-17 100 views
1

我有这个脚本通过gmail smtp发送邮件到我的gmail帐户。它不工作,并提供已经提到的错误?连接失败:IO :: Socket :: INET:connect:timeout在Perl中显示。

use Net::SMTP::TLS; 

my $mailer = new Net::SMTP::TLS( 
    'smtp.gmail.com', 
    Hello =>  'smtp.gmail.com', 
    Port =>  587, 
    User =>  '[email protected]', 
    Password=>  'xxxxxx'); 

$mailer->mail('[email protected]'); 

$mailer->to('[email protected]'); 

$mailer->data; 

$mailer->datasend("Sent from perl!"); 

$mailer->dataend; 

$mailer->quit; 

回答

3

试试这个:

use strict; 
use warnings; 

use Email::Simple; 
use Email::Sender::Simple qw(sendmail); 
use Email::Sender::Transport::SMTP::TLS; 

my $transport = Email::Sender::Transport::SMTP::TLS->new(
    host  => 'smtp.gmail.com', 
    port  => 587, 
    username => '[email protected]', 
    password => 'xxxxxx' 
); 

my $message = Email::Simple->create(
    header => [ 
     From   => '[email protected]', 
     To    => '[email protected]', 
     Subject  => 'Sent from perl!', 
    ], 
    body => 'Sent from perl!', 
); 

sendmail($message, {transport => $transport}); 
+0

谢谢斯塔姆........ – kanwarpal 2012-02-17 14:00:15

1

这个脚本其实应该工作(我用我自己的Gmail帐户测试成功)。

我怀疑你之间有一些防火墙阻止你连接到Gmail。 您可以尝试从您的主机telnet smtp.gmail.com 587。你应该有这样的事情:

host$ telnet smtp.gmail.com 587 
Trying 173.194.67.108... 
Connected to gmail-smtp-msa.l.google.com. 
Escape character is '^]'. 
220 mx.google.com ESMTP da8sm6658151wib.6 

如果没有最后的3条线,这意味着你不能直接连接到Gmail服务器。然后检查你自己的防火墙设置(如果有的话)。