Windows 通过Powershell脚本发送电子邮件时出错
我正试图发送一封带有Powershell脚本的电子邮件,当我按原样运行程序时,我收到如下错误:Windows 通过Powershell脚本发送电子邮件时出错,windows,powershell,windows-10,Windows,Powershell,Windows 10,我正试图发送一封带有Powershell脚本的电子邮件,当我按原样运行程序时,我收到如下错误: Exception setting "Add": "Cannot set the Value property for PSMemberInfo object of type "System.Management.Automation.PSMethod"." At C:\Users\documents\eventSender.ps1:11 char:1 + $emailMessageObject.T
Exception setting "Add": "Cannot set the Value property for PSMemberInfo object of type
"System.Management.Automation.PSMethod"."
At C:\Users\documents\eventSender.ps1:11 char:1
+ $emailMessageObject.To.Add = $toEmailAddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
'Attachments' is a ReadOnly property.
At C:\Users\documents\eventSender.ps1:14 char:1
+ $emailMessageObject.Attachments = $currentAttachment
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Exception calling "Send" with "1" argument(s): "A recipient must be specified."
At C:\Users\documents\eventSender.ps1:18 char:1
+ $smtpClient.Send($emailMessageObject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
$todaysDate = Get-Date -DisplayHint Date
$smtpServerAddress = "smtp.google.com"
$toEmailAddress = "admins@me.com"
$fromEmailAddress = "me@me.com"
$subjectMessage = "Files for date -> $todaysDate"
$body = "log files sent on $todaysDate (this is a test)"
$currentAttachment = "C:/Users/Downloads/TBG-1.htm"
$emailMessageObject = New-Object System.Net.Mail.MailMessage
$emailMessageObject.From = $fromEmailAddress
$emailMessageObject.To.Add = $toEmailAddress
$emailMessageObject.Subject = $subjectMessage
$emailMessageObject.Body = $body
$emailMessageObject.Attachments = $currentAttachment
$smtpClient = New-Object Net.Mail.SmtpClient($smtpServerAddress, 587)
$smtpClient.EnableSsl = $true
$smtpClient.Send($emailMessageObject)
现在,我是powershell的新手,因此我不太了解错误,我试图发送的消息是这样的:
Exception setting "Add": "Cannot set the Value property for PSMemberInfo object of type
"System.Management.Automation.PSMethod"."
At C:\Users\documents\eventSender.ps1:11 char:1
+ $emailMessageObject.To.Add = $toEmailAddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
'Attachments' is a ReadOnly property.
At C:\Users\documents\eventSender.ps1:14 char:1
+ $emailMessageObject.Attachments = $currentAttachment
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Exception calling "Send" with "1" argument(s): "A recipient must be specified."
At C:\Users\documents\eventSender.ps1:18 char:1
+ $smtpClient.Send($emailMessageObject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
$todaysDate = Get-Date -DisplayHint Date
$smtpServerAddress = "smtp.google.com"
$toEmailAddress = "admins@me.com"
$fromEmailAddress = "me@me.com"
$subjectMessage = "Files for date -> $todaysDate"
$body = "log files sent on $todaysDate (this is a test)"
$currentAttachment = "C:/Users/Downloads/TBG-1.htm"
$emailMessageObject = New-Object System.Net.Mail.MailMessage
$emailMessageObject.From = $fromEmailAddress
$emailMessageObject.To.Add = $toEmailAddress
$emailMessageObject.Subject = $subjectMessage
$emailMessageObject.Body = $body
$emailMessageObject.Attachments = $currentAttachment
$smtpClient = New-Object Net.Mail.SmtpClient($smtpServerAddress, 587)
$smtpClient.EnableSsl = $true
$smtpClient.Send($emailMessageObject)
我收到此错误消息的位置有何错误?我是否需要具有与我的电子邮件地址匹配的凭据才能发送此错误消息?您应该使用send MailMessage代替System.Net.Mail.message
我发现最简单的方法是通过挥洒:
$Splat = @{
To =$toEmailAddress
Body =$body
Subject =$subjectMessage
SmtpServer =$smtpServerAddress
From =$fromEmailAddress
Attachements = $currentAttachment
}
Send-MailMessage @Splat
如果exchange要求对发件人进行授权,则只需指定凭据。如果是这样,您只需将$Splat更改为:
MailMessage.To.Add是一个方法,因此必须像$emailMessageObject.To.Add$toEmailAddress这样调用它
MailMessage.Attachments也是一个集合,因此您必须使用$emailMessageObject.Attachments.add$currentAttachment添加该项目