簡體   English   中英

當我嘗試使用MIME :: Lite向Gmail帳戶發送電子郵件時,為什么會出現“SMTP無法連接到郵件服務器:”?

[英]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?

我有以下代碼在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\n";

當我運行它時,我收到以下錯誤:

SMTP Failed to connect to mail server:

當我用參數調用$msg->send時(參見上面的注釋行)我收到以下錯誤:

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

我怎樣才能解決這個問題?

有人在幾年前提交了一份錯誤報告 維護者的回應是:

這不太可能修復。

MIME :: Lite不支持Net :: SMTP :: TLS,我不認為自己將來會實現這一點。 我強烈建議將MIME :: Lite轉移到Email :: Sender和Email :: MIME等工具或其他更受支持的工具。

請注意,您首先不應該使用MIME::Lite ,因為文檔建議不要使用它:

等待!

其當前維護者不推薦使用MIME :: Lite。 有許多替代方案,例如Email :: MIME或MIME :: Entity和Email :: Sender,您可能應該使用它們。 MIME :: Lite繼續產生奇怪的錯誤報告,並且由於有更好的替代方案,它沒有收到大量的重構。 請考慮使用其他東西。

不推薦使用MIME :: Lite(如ThisSuitIsNotBlack說明)。

這適用於我,使用首選的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\n",
);

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

它可以使用Net :: SMTP 3.05(CPAN的最新版本)修復。 它支持SMTPS和STARTTLS。
[ 警告:請參閱MIME :: Lite 3.030 - 帶有smtps的NET :: SMTP(端口465) ]

# It should work with Net::SMTP 3.05
# MIME::Lite will pass SSL=>1 to Net::SMTP constructor
$msg->send('smtp', "smtp.gmail.com", SSL=>1,
AuthUser=>"myid@domain.com", AuthPass=>"mypass" );

以上工作時間

  1. 我用我的Gmail作為SMTP - >打賭你不認為你能做到這一點。
my $smtpserver  = 'smtp.gmail.com.';
my $smtpport     = 587;
my $smtpuser     = 'YourGmailHere@gmail.com';
my $smtppassword = 'password'; ## Plug in your password here
  1. 然后,您必須將您的Gmail帳戶的安全性設置為不太安全...基本上將“安全性較低的應用訪問”設置為“開啟”設置 - > https://myaccount.google.com/security

希望這有助於其他人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM