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

问题描述 投票:1回答:3
use warnings;
use MIME::Lite;
use Net::SMTP;

### Adjust sender, recipient and your SMTP mailhost
my $from_address = '[email protected]';
my $to_address = '[email protected]';
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=>'[email protected]',AuthPass=>'******',Debug=>1,Port=>587,Timeout=>60);
$msg->send;

该命令的输出是:

##Here's the output

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

如何解决这个问题?我坚持了。

perl email office365
3个回答
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]上的anwers。


0
投票

最重要的是,我补充道

use Net::SMTP::TLS;

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

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

它就像一个魅力!


0
投票

对我来说,上面的解决方案,使用Net::SMTP::TLS,不适用于使用MIME::Lite和Office365发送电子邮件。我收到以下错误消息:

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

然而,使用hereNet::SMTPS的解决方案,就像一个魅力。

这是为我工作的代码片段:

my $email = MIME::Lite->new(From    => 'John Doe <[email protected]>',
                             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 = '[email protected]';
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;

希望这有助于某人。

© www.soinside.com 2019 - 2024. All rights reserved.