我编写了一个简单的 Perl 脚本来使用 MIME::Lite 和 Net::SMTP 模块发送邮件。但它导致错误,“SMTP 无法连接到邮件服务器:无法立即完成非阻塞套接字操作”。
我尝试过的Perl脚本,
use strict;
use warnings;
use MIME::Lite;
use Net::SMTP;
my $from = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1071777e756377626563647566757e50777d71797c3e737f7d" rel="noreferrer noopener nofollow">[email protected]</a>';
my $to = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f495939a918793868187809182919ab49399959d98da979b99" rel="noreferrer noopener nofollow">[email protected]</a>';
my $host = 'smtp.gmail.com';
my $subject = 'A test message by script ';
my $body = "Please ignore this message as this is test from Perl scripts ";
my $msg = MIME::Lite->new (
From => $from,
To => $to,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
$msg->attach (
Type => 'TEXT',
Data => $body
) or die "Error adding the text message part: $!\n";
MIME::Lite->send('smtp', $host, Timeout=>60);
$msg->send;
我得到的实际错误,
G:\>perl SampleEmail.pl
SMTP Failed to connect to mail server: A non-blocking socket operation could not
be completed immediately.
at SampleEmail.pl line 26.
请帮助我具体需要检查什么才能正确发送邮件。
提前致谢
最佳答案
MIME::Lite/Net::SMTP v3.* - 通过 smtp.gmail.com 发送
问题1:您可以远程登录 smtp.gmail.com
上的 smtp (25) 端口吗?您收到 SMTP 问候消息吗?
telnet smtp.gmail.com 25
A1:否 => 您到 smtp 端口的传出连接很可能被阻止,以避免传出垃圾邮件。
问题2:您可以远程登录 smtp.gmail.com
上的 msa (587) 端口吗?您收到 SMTP 问候消息吗?
telnet smtp.gmail.com 587
A1:是 => 通过 msa (587) 或 smtps (465) 发送。 MIME::Lite
可以使用 Net::SMTP
版本 3.* 连接到 smtps (465) 端口。
[警告:请参阅MIME::Lite 3.030 - NET::SMTP with smtps (port 465) ]
# use smtps (465) port - it requires Net::SMTP version 3.*
# use debugging (Debug=>1) for initial tests
MIME::Lite->send('smtp', $host, SSL => 1, Timeout => 60,
AuthUser => $user, AuthPass => $pass,
Debug => 1);
关于windows - Perl:SMTP 无法连接到邮件服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35316699/