Email 发送邮件:SMTP服务器需要安全连接,或者客户端未通过身份验证。这个
第一次海报-我整个星期都在谷歌上搜索这个!我是Powershell的新手,我正在尝试利用Send MailMessage。目标是在计划任务上设置Powershell脚本以发送自动电子邮件。我知道将Powershell脚本设置为计划任务有它自己的细微差别,我已经研究过了,我想我知道接下来该怎么做,但在我达到这一点之前,我在调用脚本时一直遇到以下错误: 发送邮件:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器 答复为:5.7.57 SMTP;客户端未通过身份验证,无法在从发送邮件期间发送匿名邮件 我正在尝试使用Office 365发送邮件(从SMTP服务器可以看到)。当我将脚本直接复制并粘贴到控制台中时,它工作得很好,但是当我尝试使用以下命令调用脚本时,它会显示上面的错误Email 发送邮件:SMTP服务器需要安全连接,或者客户端未通过身份验证。这个,email,powershell,office365,Email,Powershell,Office365,第一次海报-我整个星期都在谷歌上搜索这个!我是Powershell的新手,我正在尝试利用Send MailMessage。目标是在计划任务上设置Powershell脚本以发送自动电子邮件。我知道将Powershell脚本设置为计划任务有它自己的细微差别,我已经研究过了,我想我知道接下来该怎么做,但在我达到这一点之前,我在调用脚本时一直遇到以下错误: 发送邮件:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器 答复为:5.7.57 SMTP;客户端未通过身份验证,无法在从发送邮件期间发
Powershell.exe -File C:\my_path\Script.ps1
有什么我遗漏的吗?可能是调用对其进行身份验证的脚本的更好方法吗
任何帮助都将不胜感激,我已经盯着各种论坛帖子好几天了!:) 请查看我的Github要点,或者查看下面的示例 我也有同样的问题,这似乎是一个一般性错误消息,大多数时候我在输入错误密码时收到此消息,但我相信没有SendAs权限会出现相同的错误,示例中的端口、SMTPServer和UseSSL参数都是为我刚刚测试过的Office365配置的,并且可以正常工作 还请注意,我提供了一个安全字符串的示例,如果您希望在脚本文件中安全地存储密码,则应使用此示例
### Script Global Settings
#Declare SMTP Connection Settings
$SMTPConnection = @{
#Use Office365, Gmail, Other or OnPremise SMTP Relay FQDN
SmtpServer = 'outlook.office365.com'
#OnPrem SMTP Relay usually uses port 25 without SSL
#Other Public SMTP Relays usually use SSL with a specific port such as 587 or 443
Port = 587
UseSsl = $true
#Option A: Query for Credential at run time.
Credential = Get-Credential -Message 'Enter SMTP Login' -UserName "emailaddress@domain.tld"
<#
#Option B: Hardcoded Credential based on a SecureString
Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList @(
#The SMTP User Emailaddress
"emailaddress@domain.tld"
#The Password as SecureString encoded by the user that wil run this script!
#To create a SecureString Use the folowing Command: Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
"Enter the SecureString here as a single line" | ConvertTo-SecureString
)
#>
}
### Script Variables
#Declare Mailmessages.
$MailMessageA = @{
From = "emailaddress@domain.tld"
To = @(
"emailaddress@domain.tld"
)
#Cc = @(
# "emailaddress@domain.tld"
#)
#Bcc = @(
# "emailaddress@domain.tld"
#)
Subject = 'Mailmessage from script'
#Priority = 'Normal' #Normal by default, options: High, Low, Normal
#Attachments = @(
#'FilePath'
#)
#InlineAttachments = @{
#'CIDA'='FilePath'
#} #For more information about inline attachments in mailmessages see: https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d
BodyAsHtml = $true
Body = "Something Unexpected Occured as no Content has been Provided for this Mail Message!" #Default Message
}
### Script Start
#Retrieve Powershell Version Information and store it as HTML with Special CSS Class
$PSVersionTable_HTLM = ($PSVersionTable.Values | ConvertTo-Html -Fragment) -replace '<table>', '<table class="table">'
#Retrieve CSS Stylesheet
$CSS = Invoke-WebRequest "https://raw.githubusercontent.com/advancedrei/BootstrapForEmail/master/Stylesheet/bootstrap-email.min.css" | Select-Object -ExpandProperty Content
#Build HTML Mail Message and Apply it to the MailMessage HashTable
$MailMessageA.Body = ConvertTo-Html -Title $MailMessageA.Subject -Head "<style>$($CSS)</style>" -Body "
<p>
Hello World,
</p>
<p>
If your recieved this message then this script works.</br>
</br>
<div class='alert alert-info' role='alert'>
Powershell version
</div>
$($PSVersionTable_HTLM)
</P>
" | Out-String
#Send MailMessage
#This example uses the HashTable's with a technique called Splatting to match/bind the Key's in the HashTable with the Parameters of the command.
#Use the @ Symbol instead of $ to invoke Splatting, Splatting improves readability and allows for better management and reuse of variables
Send-MailMessage @SMTPConnection @MailMessageA
###脚本全局设置
#声明SMTP连接设置
$SMTPConnection=@{
#使用Office365、Gmail、其他或本地SMTP中继FQDN
SmtpServer='outlook.office365.com'
#OnPrem SMTP中继通常使用不带SSL的端口25
#其他公共SMTP中继通常使用SSL和特定端口,如587或443
端口=587
usesl=$true
#选项A:在运行时查询凭据。
凭据=获取凭据-消息“输入SMTP登录”-用户名emailaddress@domain.tld"
}
###脚本变量
#声明邮件消息。
$MailMessageA=@{
From=”emailaddress@domain.tld"
至=@(
"emailaddress@domain.tld"
)
#Cc=@(
# "emailaddress@domain.tld"
#)
#密件抄送=@(
# "emailaddress@domain.tld"
#)
主题='Mailmessage from script'
#优先级=‘正常’#默认为正常,选项:高、低、正常
#附件=@(
#“文件路径”
#)
#内联附件=@{
#“CIDA”=“文件路径”
#}#有关邮件中内联附件的更多信息,请参阅:https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d
BodyAsHtml=$true
Body=“由于没有为此邮件消息提供任何内容,因此发生了意外事件!”#默认消息
}
###脚本开始
#检索Powershell版本信息并使用特殊的CSS类将其存储为HTML
$PSVersionTable_HTLM=($PSVersionTable.Values |转换为Html-片段)-替换“”,“”
#检索CSS样式表
$CSS=调用WebRequest“https://raw.githubusercontent.com/advancedrei/BootstrapForEmail/master/Stylesheet/bootstrap-email.min.css“|选择对象-扩展属性内容
#构建HTML邮件消息并将其应用于邮件消息哈希表
$MailMessageA.Body=转换为Html-Title$MailMessageA.Subject-Head“$($CSS)”-Body
你好,世界,
如果您收到此消息,则此脚本有效
Powershell版本
$($pVersionTable\u HTLM)
“|输出字符串
#发送邮件
#本例使用哈希表的和一种称为Splatting的技术来匹配/绑定哈希表中的键和命令的参数。
#使用@符号而不是$来调用Splatting,Splatting提高了可读性,并允许更好地管理和重用变量
发送MailMessage@SMTPConnection@MailMessageA
### Script Global Settings
#Declare SMTP Connection Settings
$SMTPConnection = @{
#Use Office365, Gmail, Other or OnPremise SMTP Relay FQDN
SmtpServer = 'outlook.office365.com'
#OnPrem SMTP Relay usually uses port 25 without SSL
#Other Public SMTP Relays usually use SSL with a specific port such as 587 or 443
Port = 587
UseSsl = $true
#Option A: Query for Credential at run time.
Credential = Get-Credential -Message 'Enter SMTP Login' -UserName "emailaddress@domain.tld"
<#
#Option B: Hardcoded Credential based on a SecureString
Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList @(
#The SMTP User Emailaddress
"emailaddress@domain.tld"
#The Password as SecureString encoded by the user that wil run this script!
#To create a SecureString Use the folowing Command: Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
"Enter the SecureString here as a single line" | ConvertTo-SecureString
)
#>
}
### Script Variables
#Declare Mailmessages.
$MailMessageA = @{
From = "emailaddress@domain.tld"
To = @(
"emailaddress@domain.tld"
)
#Cc = @(
# "emailaddress@domain.tld"
#)
#Bcc = @(
# "emailaddress@domain.tld"
#)
Subject = 'Mailmessage from script'
#Priority = 'Normal' #Normal by default, options: High, Low, Normal
#Attachments = @(
#'FilePath'
#)
#InlineAttachments = @{
#'CIDA'='FilePath'
#} #For more information about inline attachments in mailmessages see: https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d
BodyAsHtml = $true
Body = "Something Unexpected Occured as no Content has been Provided for this Mail Message!" #Default Message
}
### Script Start
#Retrieve Powershell Version Information and store it as HTML with Special CSS Class
$PSVersionTable_HTLM = ($PSVersionTable.Values | ConvertTo-Html -Fragment) -replace '<table>', '<table class="table">'
#Retrieve CSS Stylesheet
$CSS = Invoke-WebRequest "https://raw.githubusercontent.com/advancedrei/BootstrapForEmail/master/Stylesheet/bootstrap-email.min.css" | Select-Object -ExpandProperty Content
#Build HTML Mail Message and Apply it to the MailMessage HashTable
$MailMessageA.Body = ConvertTo-Html -Title $MailMessageA.Subject -Head "<style>$($CSS)</style>" -Body "
<p>
Hello World,
</p>
<p>
If your recieved this message then this script works.</br>
</br>
<div class='alert alert-info' role='alert'>
Powershell version
</div>
$($PSVersionTable_HTLM)
</P>
" | Out-String
#Send MailMessage
#This example uses the HashTable's with a technique called Splatting to match/bind the Key's in the HashTable with the Parameters of the command.
#Use the @ Symbol instead of $ to invoke Splatting, Splatting improves readability and allows for better management and reuse of variables
Send-MailMessage @SMTPConnection @MailMessageA