2017-10-16 859 views
0
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; 

命令的输出是:SMTP认证()命令不支持smtp.office365.com

##Here's the output 

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

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

回答

0

首先,我加

use Net::SMTP::TLS; 

后MIME ::精简版的代码,我用下面的邮件:

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(核心模块),并执行'$ mailer-> starttls() '建立连接后。少一个模块来安装。另外,您可以简单地执行'$ mailer-> data($ msg-> as_string)'并跳过'datesend'和'dataend'。 –

+0

两个竖起大拇指给你bro @SteffenUllrich –

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

您正在尝试使用不带SSL的身份验证。出于安全原因,smtp.office365.com不支持此功能,这可以在握手中看到。初始连接显示可用的AUTH后EHLO响应:升级到TLS认证方法时

<<< 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 

只得到可供选择:

>>> 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 ::精简版不支持使用的starttls。请参阅MIME::Lite - Cannot send mail [SMTP auth() command not supported on smtp.gmail.com]上的anwers了解替代方案。

+0

我已经遵循和编辑我的代码来添加SSL => 1并使用NET :: SMTP(3.10),但仍然无法正常工作。那么发送电子邮件到office365的最佳方式是什么? –

+0

@DoniAndriCahyono:'SSL => 1'不使用显式TLS(即starttls),而使用隐式TLS。引用自己:*“不幸的是,它看起来像MIME :: Lite不支持starttls的使用。请参阅MIME :: Lite上的anwers - 不能发送邮件....替代*”。 –

+0

'MIME :: Lite'不会将'SSL'选项传递给'Net :: SMTP' - 请参阅https://stackoverflow.com/questions/45588971/mimelite-3-030-netsmtp-with-smtps-port-465 – AnFi

0

对于我来说,使用Net::SMTP::TLS的上述解决方案不适用于使用MIME::Lite & 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 <[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; 

希望这可以帮助别人。