为什么在尝试使用powershell脚本发送电子邮件时出错
好的,我正在尝试使用powershell v2向一些收件人发送一封电子邮件,发送的文本文件名为emailtargets,格式为新行。下面是sendmailer脚本为什么在尝试使用powershell脚本发送电子邮件时出错,powershell,powershell-2.0,Powershell,Powershell 2.0,好的,我正在尝试使用powershell v2向一些收件人发送一封电子邮件,发送的文本文件名为emailtargets,格式为新行。下面是sendmailer脚本 $emailSmtpServer = "mail.xxxxxxxxx.com" $emailSmtpServerPort = "587" $SourceRCPT = "xxxxx@xxxxxxxxx.com" $DestFile = "emailTargets.txt" $BodyFile = "emailBody.txt" $Sub
$emailSmtpServer = "mail.xxxxxxxxx.com"
$emailSmtpServerPort = "587"
$SourceRCPT = "xxxxx@xxxxxxxxx.com"
$DestFile = "emailTargets.txt"
$BodyFile = "emailBody.txt"
$SubjectLine = "testing"
$Pass = "xxxxxxx"
# Start Loop for Email Sending
$file = Get-Content $DestFile
$body = Get-Content $BodyFile
foreach ($line in $file){
$DestinationRCPT = $line.Split(",")[0]
$firstName = $line.Split(",")[1]
$lastName = $line.Split(",")[2]
$body =(Get-Content $BodyFile) | foreach-object {$_ -replace '\[firstName\]',$firstName}| foreach-object {$_ -replace '\[lastName\]',$lastName}
$emailSmtpUser = "$SourceRCPT"
$emailSmtpPass = "$Pass"
$emailFrom = "$SourceRCPT"
$emailTo = "$DestinationRCPT"
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = $SubjectLine
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $body
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser.Split("@")[0] , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
Write-Host "Sending Email to $DestinationRCPT"
现在的问题是,在使用office 365进行测试时,我一直会遇到下面的错误
在使用不同的smtp服务器进行测试时,我发现以下错误
我做错了什么?这是一个证书验证问题,如果您可以抑制它,您可以这样做
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
在发送消息之前。这里有一组示例,可以帮助您将此消息重新写入函数 Windows PowerShell Windows中可用的cmdlet概述 PowerShell 2.0 用这个例子 或 #使用PS内置/联机帮助文件
Get-Command -Name Send-MailMessage
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Send-MailMessage
3.1.0.0 Microsoft.PowerShell.Utility
# get function / cmdlet details
(Get-Command -Name Send-MailMessage).Parameters.Keys
Attachments
Bcc
Body
BodyAsHtml
Encoding
Cc
DeliveryNotificationOption
From
SmtpServer
Priority
Subject
To
Credential
UseSsl
Port
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
# Get the examples and modify per your use case, splatting used to make it easier to read on the forum.
Get-help -Name Send-MailMessage -Examples
# Example 1: Send an email from one user to another
$Params = @{
To = "User01 <user01@example.com>"
From = "User02 <user02@example.com>"
Subject = "Test mail"
Credential = (Get-Credential -Credential 'user@fabrikam.com')
SmtpServer = "smtp.fabrikam.com"
UseSsl = $true
Port = 587
}
Send-MailMessage @Params
# This command sends an email message from User01 to User02.
#
# The mail message has a subject, which is required, but it does not have a
# body, which is optional. Also, because the SmtpServer parameter is not
# specified,
#
# Send-MailMessage uses the value of the $PSEmailServer preference variable
# for the SMTP server.
# Example 2: Send an attachment
$Params = @{
From = "User01 <user01@example.com>"
To = "User02 <user02@example.com>", "User03 <user03@example.com>"
Subject = "Sending the Attachment"
Body = "Forgot to send the attachment. Sending now."
Attachments = "data.csv"
Priority = High
dno = 'onSuccess, onFailure'
Credential = (Get-Credential -Credential 'user@fabrikam.com')
SmtpServer = "smtp.fabrikam.com"
UseSsl = $true
Port = 587
}
Send-MailMessage @Params
# This command sends an email message with an attachment from User01 to two
# other users.
# It specifies a priority value of High and requests a delivery notification
# by email when the email messages are delivered or when they fail.
# Example 3: Send email to a mailing list
$Params = @{
To = "User01 <user01@example.com>"
From = "ITGroup <itdept@example.com>"
Cc = "User02 <user02@example.com>"
bcc = "ITMgr <itmgr@example.com>"
Subject = "Don't forget today's meeting!"
Credential = 'domain01\admin01'
UseSsl = $true
SmtpServer = "smtp.fabrikam.com"
Port = 587
}
Send-MailMessage @Params
# This command sends an email message from User01 to the ITGroup mailing list
# with a copy (Cc) to User02 and a blind carbon copy (Bcc) to the IT manager
# (ITMgr).
#
# The command uses the credentials of a domain administrator and the UseSsl
# parameter.
Get命令-Name发送邮件消息
CommandType名称版本源
----------- ---- ------- ------
Cmdlet发送邮件消息
3.1.0.0 Microsoft.PowerShell.Utility
#获取函数/cmdlet详细信息
(Get命令-Name Send MailMessage).Parameters.Keys
附件
密件抄送
身体
BodyAsHtml
编码
复写的副本
DeliveryNotificationOption
从…起
SmtpServer
优先
主题
到
资质
使用SSL
港口
冗长的
调试
错误行为
警告行动
信息行动
误差变量
警告变量
信息变量
外变量
爆发者
管道变量
#获取示例并根据您的用例进行修改,使用splatting使其更易于在论坛上阅读。
获取帮助-名称发送邮件-示例
#示例1:从一个用户向另一个用户发送电子邮件
$Params=@{
To=“User01”
From=“User02”
主题=“测试邮件”
凭证=(获取凭证-凭证'user@fabrikam.com')
SmtpServer=“smtp.fabrikam.com”
usesl=$true
端口=587
}
发送MailMessage@Params
#此命令从User01向User02发送电子邮件消息。
#
#邮件消息有一个主题,这是必需的,但它没有主题
#主体,这是可选的。另外,因为SmtpServer参数不是
#指明,,
#
#Send MailMessage使用$PSEmailServer首选项变量的值
#对于SMTP服务器。
#示例2:发送附件
$Params=@{
From=“User01”
To=“User02”,“User03”
主题=“发送附件”
Body=“忘记发送附件。现在发送。”
附件=“data.csv”
优先级=高
dno='onSuccess,onFailure'
凭证=(获取凭证-凭证'user@fabrikam.com')
SmtpServer=“smtp.fabrikam.com”
usesl=$true
端口=587
}
发送MailMessage@Params
#此命令从User01向两个用户发送带有附件的电子邮件
#其他用户。
#它指定高优先级值并请求传递通知
#在电子邮件发送或失败时通过电子邮件发送。
#示例3:向邮件列表发送电子邮件
$Params=@{
To=“User01”
From=“ITGroup”
Cc=“User02”
bcc=“ITMgr”
主题=“别忘了今天的会议!”
凭证='domain01\admin01'
usesl=$true
SmtpServer=“smtp.fabrikam.com”
端口=587
}
发送MailMessage@Params
#此命令从User01向ITGroup邮件列表发送电子邮件
#向User02发送副本(抄送),向IT经理发送盲副本(密件抄送)
#(资讯科技及广播事务经理)。
#
#该命令使用域管理员和UseSsl的凭据
#参数。
获取帮助-名称发送邮件-完整
Get help-Name Send MailMessage-Onlinev2中是否不存在
Send MailMessage
功能?@TheIncorrigible1事实上我对这一功能非常陌生,仍在尝试四处走动。我想是的,但是我应该在脚本中添加到哪里呢?您不需要手动构造smtp/mail对象,只需使用Send MailMessage
。签出Get Help-Name Send MailMessage
@africonso第一个错误(来自O365)表明您提供的凭据无效,第二个错误表示客户端不信任smtp服务器使用的证书computer@TheIncorrigible1我再次尝试并添加了sendmailmessage命令,但仍然无法工作。请你编辑脚本的方式应该让我看看?
Get-Command -Name Send-MailMessage
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Send-MailMessage
3.1.0.0 Microsoft.PowerShell.Utility
# get function / cmdlet details
(Get-Command -Name Send-MailMessage).Parameters.Keys
Attachments
Bcc
Body
BodyAsHtml
Encoding
Cc
DeliveryNotificationOption
From
SmtpServer
Priority
Subject
To
Credential
UseSsl
Port
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
# Get the examples and modify per your use case, splatting used to make it easier to read on the forum.
Get-help -Name Send-MailMessage -Examples
# Example 1: Send an email from one user to another
$Params = @{
To = "User01 <user01@example.com>"
From = "User02 <user02@example.com>"
Subject = "Test mail"
Credential = (Get-Credential -Credential 'user@fabrikam.com')
SmtpServer = "smtp.fabrikam.com"
UseSsl = $true
Port = 587
}
Send-MailMessage @Params
# This command sends an email message from User01 to User02.
#
# The mail message has a subject, which is required, but it does not have a
# body, which is optional. Also, because the SmtpServer parameter is not
# specified,
#
# Send-MailMessage uses the value of the $PSEmailServer preference variable
# for the SMTP server.
# Example 2: Send an attachment
$Params = @{
From = "User01 <user01@example.com>"
To = "User02 <user02@example.com>", "User03 <user03@example.com>"
Subject = "Sending the Attachment"
Body = "Forgot to send the attachment. Sending now."
Attachments = "data.csv"
Priority = High
dno = 'onSuccess, onFailure'
Credential = (Get-Credential -Credential 'user@fabrikam.com')
SmtpServer = "smtp.fabrikam.com"
UseSsl = $true
Port = 587
}
Send-MailMessage @Params
# This command sends an email message with an attachment from User01 to two
# other users.
# It specifies a priority value of High and requests a delivery notification
# by email when the email messages are delivered or when they fail.
# Example 3: Send email to a mailing list
$Params = @{
To = "User01 <user01@example.com>"
From = "ITGroup <itdept@example.com>"
Cc = "User02 <user02@example.com>"
bcc = "ITMgr <itmgr@example.com>"
Subject = "Don't forget today's meeting!"
Credential = 'domain01\admin01'
UseSsl = $true
SmtpServer = "smtp.fabrikam.com"
Port = 587
}
Send-MailMessage @Params
# This command sends an email message from User01 to the ITGroup mailing list
# with a copy (Cc) to User02 and a blind carbon copy (Bcc) to the IT manager
# (ITMgr).
#
# The command uses the credentials of a domain administrator and the UseSsl
# parameter.