【问题标题】:(perl Net::SMTP) authentication failure using gmail smtp server(perl Net::SMTP) 使用 gmail smtp 服务器的身份验证失败
【发布时间】:2012-06-16 02:43:32
【问题描述】:

我编写了一个 perl 脚本来使用 gmail 的 smtp 服务器发送电子邮件:smtp.gmail.com -

use Net::SMTP;

my $smtp = Net::SMTP->new('smtp.gmail.com',
                    Port=> 587,
                    Timeout => 20,
                    Hello=>'user@gmail.com'
                    );
print $smtp->domain,"\n";
$sender = "user@gmail.com";
$password = "mypassword";
$smtp->auth ( $sender, $password ) or die "could not authenticate\n";
$receiver = "user@gmail.com";

$subject = "my custom subject";
$smtp->mail($sender);
$smtp->to($receiver);
$smtp->data();
$smtp->datasend("To: <$reciever> \n");
$smtp->datasend("From: <$sender> \n");
$smtp->datasend("Content-Type: text/html \n");
$smtp->datasend("Subject: $subject");
$smtp->datasend("\n");
$smtp->datasend('the body of the email');
$smtp->dataend();
$smtp->quit();
print "done\n\n";

对于“用户”,我有我的 gmail 用户名,对于“我的密码”,我有密码。但是当我运行这段代码时,它会在身份验证本身停止,给出:无法身份验证。

我能够连接到 google 的 smtp 服务器,因为我得到:'mx.google.com',因为:
打印 $smtp->domain,"\n";

我做错了什么?请帮忙。

【问题讨论】:

    标签: perl smtp gmail


    【解决方案1】:
    use Net::SMTP;
    my $smtp = Net::SMTP->new ....
    

    afaik 您需要使用加密连接通过 gmail 发送,使用 Net::SMTP::TLSNet::SMTP::SSL(使用端口 465 )

    Hello=>'user@gmail.com'
    

    “你好”不是电子邮件地址,而是将您的主机名放在那里

    $sender = "user@gmail.com";
    ...
    $receiver = "user@gmail.com";
    

    将这些放在单引号中

    如果您仍然收到“无法验证”。确保您已安装模块 MIME::Base64Authen::SASL

     $smtp->datasend("To: <$reciever> \n");
    

    应该是 $receiver,而不是 $reciever

    【讨论】:

    • 我删除了 'Hello' 参数并更正了引用错误,但仍然没有结果。 TLS 和 SSL 模块可用于 perl 5.14 窗口。我不能使用它,因为我必须在 windows 中使用 win32::serialport。请通过调整上面的代码本身来告诉一些发送方式。
    • +1 用于提及“如果您仍然得到“无法进行身份验证。”请确保您已安装模块 MIME::Base64 和 Authen::SASL。”
    【解决方案2】:

    Gmail 使用TLS/STARTTLS on port 587, SSL on port 465。请关注Sending email through Google SMTP from Perl。或者,您可以使用Email::Send::SMTP::Gmail。我推荐这个,因为它更容易使用并且有更多的功能。

    【讨论】:

    • 我还必须使用 win32::serialport,这在 windows 中的 perl 5.14 中不受支持。您所指的那个仅在 windows 中支持 5.14。
    猜你喜欢
    • 2011-10-09
    • 2019-03-14
    • 2016-02-29
    • 2013-02-06
    • 2017-03-10
    • 2018-06-23
    • 2022-08-17
    • 1970-01-01
    • 2012-12-23
    相关资源
    最近更新 更多