【问题标题】:SMTP auth() command not supported on smtp.office365.comsmtp.office365.com 不支持 SMTP auth() 命令
【发布时间】:2018-03-28 02:27:41
【问题描述】:
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 

如何解决这个问题?我被困住了。

【问题讨论】:

    标签: perl email office365


    【解决方案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] 上的答案以获取替代方案。

    【讨论】:

    • 我已经按照并编辑了我的代码以添加 SSL=>1 并使用 NET::SMTP(3.10) 但仍然无法正常工作。那么向 office365 发送电子邮件的最佳方式是什么?
    • @DoniAndriCahyono: SSL =&gt; 1 不使用显式 TLS(即 starttls),而是使用隐式 TLS。引用我自己的话:“不幸的是,看起来 MIME::Lite 不支持使用 starttls。请参阅 MIME::Lite 上的答案 - 无法发送邮件 .... 以获取替代方案”。
    • MIME::Lite 没有将SSL 选项传递给Net::SMTP - 请参阅stackoverflow.com/questions/45588971/…
    • @AnFi: 再加上SSL =&gt; 1 无论如何都不是正确的选择:)
    • 对于支持 smtps(端口 465,例如 gmail)的服务来说,它看起来是个不错的解决方案 但是 MIME::Lite 从传递给Net::SMTP 构造函数的选项中过滤掉它(@987654332 @)。
    【解决方案2】:

    首先,我添加了

    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;
    

    它就像一个魅力!

    【讨论】:

    • 您也可以使用 Net::SMTP(核心模块)并在建立连接后执行$mailer-&gt;starttls()。少安装一个模块。此外,您可以简单地执行$mailer-&gt;data($msg-&gt;as_string) 并跳过datesenddataend
    • 为你竖起大拇指 @SteffenUllrich 兄弟
    【解决方案3】:

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

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

    但是,here 使用 Net::SMTPS 给出的解决方案非常有效。

    这是对我有用的代码 sn-p:

    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;
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 2015-04-04
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      相关资源
      最近更新 更多