繁体   English   中英

通过PowerShell将电子邮件发送到Office365

[英]Sending an email to office365 via PowerShell

我正在尝试一个脚本,该脚本会将输出通过电子邮件发送给我。 我大部分都可以使用,但是在发送电子邮件时需要我输入密码。 有没有办法将其添加到脚本中? 我没有用户名,但没有密码。 我相信这很简单。 基本上,我希望计算机b运行此脚本。 并通过电子邮件将信息发送回计算机,它们都位于两个不同的网络上。 如果我不知道计算机的IP b。 这是我到目前为止的内容:

$secpasswd = ConvertTo-SecureString “mypassword” -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential 
(“support@email.com.com”, $secpasswd)

Send-MailMessage -To "support@email.com" -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 "support@email.com" -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 "support@email.com" -SmtpServer "smtp.office365 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], Smtp 
   Exception
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

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

有人遇到过这样的问题吗? 我真的很想看看这是否可以完成,我知道可以,但是我缺少一些……可能很简单。

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

请参考以下内容:

我有一个巴掌的时刻。 我的凭据中的电子邮件中包含2个.com。 删除其中之一后,它可以工作。 不管怎么说,多谢拉。

对于寻找通过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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM