通过PowerShell向office365发送电子邮件

问题描述 投票:0回答:3

我正在尝试一个脚本,它会通过电子邮件将输出发送给我。我得到了大部分工作,但在发送电子邮件时,我希望我输入密码。有没有办法将其添加到脚本中?我有用户ID但没有密码。我确信这很简单。基本上我想让电脑b运行这个脚本。并通过电子邮件将信息发送给计算机,两者都在两个不同的网络上。如果我不知道计算机的IP b。这是我到目前为止:

$secpasswd = ConvertTo-SecureString “mypassword” -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential 
(“[email protected]”, $secpasswd)

Send-MailMessage -To "[email protected]" -SmtpServer "smtp.office365.com" -
Credential $mycreds -UseSsl "Backup Notification" -Port "587" -Body "This a 
Test Message.<br>Brought to you by PowerShell.<br> Hopefully this will help 
setup VPN Connections<b>Thanks</b>" -From "[email protected]" -BodyAsHtml

我通过发送电子邮件来测试这个,我似乎总是得到以下错误:

 Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server 
response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM 
[DM5PR20CA0014.namprd20.prod.outlook.com]
At C:\Users\502706436\Desktop\tight vnc\test email 1.ps1:6 char:1
+ Send-MailMessage -To "[email protected]" -SmtpServer "smtp.office365 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], Smtp 
   Exception
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

--------------------------------------------------------------------------------

有没有其他人有这样的问题?我真的想知道是否可以做到这一点,我知道它可以,但我遗漏了一些东西......可能是一件简单的事情。

powershell email office365
3个回答
0
投票

这表示您的外发邮件帐户中配置的SMTP服务器正在连接到SMTP客户端提交端点,该端点不能用于直接发送。因此,将Exchange SMTP配置为直接发送。

请参阅以下内容:


0
投票

我脸上一巴掌。我凭据中的电子邮件中有2个.com。删除其中一个后,它的工作原理。不管怎么说,多谢拉。


0
投票

对于其他任何寻找通过Outlook Office365发送电子邮件的方法的人来说:

# Sending an email from PowerShell 5.1 script through outlook.office365.com
#
# 1. Create an encrypted password file
#   PS > Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath <passwordfile>
#   This will prompt you for a password, encrypt and save in <passwordfile>
# 2. Obtain Outlook Office365 SMTP server name.
#   Go to your ISP and find the value of the MX record. For example <yourdomain>.mail.protection.outlook.com
# 3. If after running the script you get this error:
#   Send-MailMessage : Mailbox unavailable. The server response was: 5.7.606 Access denied, banned sending IP [X.X.X.X].
#   You will need to delist your IP by going here: https://sender.office.com/
#   Note:  Removing you IP from the block list could take up to 30 minutes.
#
$User = "<SMPT loging username>"
$PasswordFile = "<passwordfile>"
$SMTPCredentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString)
$EmailTo = "<to email address>"
$EmailFrom = "<from email address>"
$Subject = "<email subject>" 
$Body = "<email body>" 
$SMTPServer = "<Outlook STMP Server from MX record>"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port 25 -Credential $SMTPCredentials -UseSsl
© www.soinside.com 2019 - 2024. All rights reserved.