smtp.office365.com不支持SMTP auth()命令

时间:2017-10-16 14:15:48

标签: perl email office365

use warnings;
use MIME::Lite;
use Net::SMTP;

### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'doni@home.com';
my $to_address = 'doni@home.com';
my $mail_host = 'smtp.office365.com';

### Adjust subject and body message
my $subject = 'A message with 2 parts ...';
my $message_body = "Here's the attachment file(s) you wanted";

### Adjust the filenames
my $my_file_txt = 'C:\Users\Doni\Documents';
my $your_file_txt = 'test1.txt';

### Create the multipart container
$msg = MIME::Lite->new (
    From => $from_address,
    To => $to_address,
    Subject => $subject,
    Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

### Add the text message part
$msg->attach (
    Type => 'TEXT',
    Data => $message_body
) or die "Error adding the text message part: $!\n";

### Add the TEXT file
$msg->attach (
    Type => 'text',
    Encoding => 'base64',
    Path => $my_file_txt,
    Filename => $your_file_txt,
    Disposition => 'attachment'
) or die "Error adding file_text: $!\n";

##Send 
MIME::Lite->send('smtp',$mail_host,SSL=>0,AuthUser=>'doni@home.com',AuthPass=>'******',Debug=>1,Port=>587,Timeout=>60);
$msg->send;

命令的输出是:

##Here's the output

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

如何解决该问题?我坚持不懈。

3 个答案:

答案 0 :(得分:1)

MIME::Lite->send('smtp',$mail_host,SSL=>0,AuthUser=>...,Port=>587 ...
                                   ^^^^^^  ^^^^^^

您正在尝试使用不使用SSL的身份验证。出于安全原因,smtp.office365.com不支持此操作,如握手中所示。初始连接后对EHLO的响应显示没有AUTH可用:

<<< 220 LNXP265CA0010.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 16 Oct 2017 14:36:53 +0000
>>> EHLO ...
<<< 250-LNXP265CA0010.outlook.office365.com Hello ...
<<< 250-SIZE 157286400
<<< 250-PIPELINING
<<< 250-DSN
<<< 250-ENHANCEDSTATUSCODES
<<< 250-STARTTLS
<<< 250-8BITMIME
<<< 250-BINARYMIME
<<< 250-CHUNKING
<<< 250 SMTPUTF8

只有在升级到TLS AUTH时才可用:

>>> STARTTLS
<<< 220 2.0.0 SMTP server ready
>>> EHLO localhost.localdomain
<<< 250-LNXP265CA0010.outlook.office365.com Hello ...
<<< 250-SIZE 157286400
<<< 250-PIPELINING
<<< 250-DSN
<<< 250-ENHANCEDSTATUSCODES
<<< 250-AUTH LOGIN XOAUTH2          <<<<<<<<<<<<< HERE
<<< 250-8BITMIME
<<< 250-BINARYMIME
<<< 250-CHUNKING
<<< 250 SMTPUTF8

不幸的是,看起来MIME :: Lite不支持使用starttls。有关替代方案,请参阅MIME::Lite - Cannot send mail [SMTP auth() command not supported on smtp.gmail.com]上的答案。

答案 1 :(得分:0)

首先,我添加了

use Net::SMTP::TLS;

在MIME :: Lite代码之后,我使用了以下邮件:

my $mailer = new Net::SMTP::TLS(
    "smtp.office365.com",
    Hello   =>      'smtp.office365.com',
    Port    =>      587,
    User    =>      'doni@home.com',
    Password=>      '******');
$mailer->mail('doni@home.com');
$mailer->to('doni@home.com');
$mailer->data;
$mailer->datasend($msg->as_string);
$mailer->dataend;
$mailer->quit;

它就像一个魅力!

答案 2 :(得分:0)

对我而言,使用Net::SMTP::TLS的上述解决方案无法使用MIME::Lite&amp;发送电子邮件。 Office365。我收到以下错误消息:

invalid SSL_version specified at /usr/share/perl5/IO/Socket/SSL.pm line 575.

然而,使用Net::SMTPS给出here的解决方案,就像一个魅力。

以下是适用于我的代码段:

my $email = MIME::Lite->new(From    => 'John Doe <john.doe@example.com>',
                             To      => $to,
                             Subject => $email_subject,
                             Type    =>'multipart/related' );

$email->attach(Type => 'text/html',
               Data => $email_body);

my $smtps = Net::SMTPS->new("smtp.office365.com", Port => 587,  doSSL => 'starttls', SSL_version=>'TLSv1');

my $username = 'john.doe@example.com';
my $password = 'myc0mpl1c4t3dpa55w0rd';

$smtps->auth ( $username, $password ) or DIE("Nooooo..Could not authenticate with mail server!!\n");

$smtps->mail($username);
$smtps->to($to);
$smtps->data();
$smtps->datasend( $email->as_string() );
$smtps->dataend();
$smtps->quit;

希望这有助于某人。