Send-MailMessage PowerShell无法通过MFA进行身份验证

时间:2019-12-10 18:19:17

标签: powershell email automation

我正在尝试使用Send-MailMessage cmdlet发送电子邮件。最终目标是从CSV向用户列表发送电子邮件。但是,使用此cmdlet发送单个电子邮件时遇到麻烦。我发送的电子邮件是O365电子邮件地址,我已经查找了要使用的正确端口和smtp服务器名称,但是由于某种原因,我收到以下错误消息:

  

Send-MailMessage:SMTP服务器需要安全连接,或者   客户端未通过身份验证。服务器响应为:5.7.57 SMTP;   客户端未通过身份验证以在MAIL FROM期间发送匿名邮件   [BY5PR13CA0016.namprd13.prod.outlook.com]行:1字符:1   + Send-MailMessage -To'**************'-From $ SenderAddress -Subje ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:InvalidOperation:(System.Net.Mail.SmtpClient:SmtpClient)[Send-MailMessage],   SmtpException       + FullyQualifiedErrorId:SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

以下是我的代码。有人知道我在做什么错吗?

编辑:“我的脚本”可以运行,但不适用于MFA。我不知道如何使用MFA对服务器进行身份验证。

 #Sending Emails using PowerShell. 

#Note: remove spaces in Display Name column and SAM Account Name column. For simplicity the Display Name field has been renamed to DisplayName
#And the SAM account Name field has been renamed to SAM.

#Setting the default SMTPServer
$SmtpServer = 'smtp.office365.com'
#Prompt for the senders email address
$SenderAddress = Read-Host -Prompt "Enter Sender's Email Address: "
$SenderPassword = Read-Host -Prompt "Enter your password: "
#Prompt for Recipient Email Address
$RecepientAddress = Read-Host -Prompt "Enter the recipient's address: "
#Setting up authentication to SMTP server
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SenderAddress, $($SenderPassword | ConvertTo-SecureString -AsPlainText -Force)

#Composing the email. 
$SubjectLine = "Test Email."
$EmailBody = "Testing CMDlet"

Send-MailMessage -To $RecepientAddress -From $SenderAddress -Subject $SubjectLine -Body $EmailBody -BodyAsHtml -Priority High -DeliveryNotificationOption OnFailure, OnSuccess -SmtpServer $SmtpServer -Port 587 -UseSsl -Credential $Credentials

0 个答案:

没有答案