为什么我会收到“SMTP 无法连接到邮件服务器:"当我尝试使用 MIME::Lite 向 Gmail 帐户发送电子邮件时? [英] Why do I get "SMTP Failed to connect to mail server:" when I try to send an email to a Gmail account using MIME::Lite?

查看:9
本文介绍了为什么我会收到“SMTP 无法连接到邮件服务器:"当我尝试使用 MIME::Lite 向 Gmail 帐户发送电子邮件时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在 Perl 中发送电子邮件:

I have following code to send an email in Perl:

#!/usr/bin/perl

use MIME::Lite;

$to = 'toid@domain.com';
$cc = 'ccid@domain.com';
$from = 'fromid@domain.com';

$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
             From     => $from,
             To       => $to,
             Cc       => $cc,
             Subject  => $subject,
             Data     => $message
             );

$msg->send;
#$msg->send('smtp', "smtp.gmail.com", AuthUser=>"myid@domain.com", AuthPass=>"mypass" );
#$msg->send('smtp', "smtp.gmail.com",  Debug=>0 );
#$msg->send('type',@args);
print "Email Sent Successfully
";

当我运行它时,我收到以下错误:

When I run it I get the following error:

SMTP Failed to connect to mail server:

当我使用参数调用 $msg->send 时(请参阅上面的注释行),我收到以下错误:

When I call $msg->send with arguments (see the commented lines above) I get the following error:

SMTP auth() command not supported on smtp.gmail.com

我该如何解决这个问题?

How can I fix this?

推荐答案

不推荐使用 MIME::Lite(如 ThisSuitIsNotBlack 所述).

MIME::Lite is (as ThisSuitIsNotBlack notes) deprecated.

这对我有用,使用首选的 Email::Sender:

This works for me, using the preferred Email::Sender:

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS ();
use Email::Simple ();
use Email::Simple::Creator ();

my $smtpserver = 'server';
my $smtpport = 587;
my $smtpuser   = 'username';
my $smtppassword = 'password';

my $transport = Email::Sender::Transport::SMTPS->new({
  host => $smtpserver,
  port => $smtpport,
  ssl => "starttls",
  sasl_username => $smtpuser,
  sasl_password => $smtppassword,
});

my $email = Email::Simple->create(
  header => [
    To      => 'mymail@gmail.com',
    From    => 'sender@example.com',
    Subject => 'Hi!',
  ],
  body => "This is my message
",
);

sendmail($email, { transport => $transport });

这篇关于为什么我会收到“SMTP 无法连接到邮件服务器:"当我尝试使用 MIME::Lite 向 Gmail 帐户发送电子邮件时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆