I have a powershell script that sends an email message successfully on Win10 machines. I now need to adapt it to send the same message on Win 7, but it’s not working. Debugging shows that the process fails at the last step:
$SMTPClient.Send($SMTPMessage)
where it is returning this error:
Stopped at: if ($_.FullyQualifiedErrorId -ne “NativeCommandErrorMessage” -and $ErrorView -ne “CategoryView”) {
Displaying the contents of $_.FullyQualifiedErrorId shows the value: DotNetMethodException
Displaying the contents of $ErrorView shows the value: NormalView
The script is below. The error occurs at the same point in both IF branches, on the call:
$SMTPClient.Send($SMTPMessage)
$User = "[email protected]"
$File = "c:\m2emla93.file"
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString -AsPlainText -Force)
$EmailTo = "[email protected]"
$EmailFrom = "[email protected]"
$Subject = "SYSTEM 2 ALARM*"
$bodyfile = "c:windowssystemfacility.txt"
$Body = Get-Content $bodyFile -Raw
$SMTPServer = “mail.mydomain.com"
$path="c:windowssystemweb"
$filemask='screenstream*.jpg'
$filter=$path + $filemask
if (Test-Path $filter) {
$lastFile = Get-ChildItem -Recurse -Path $path -Include $filemask | Sort-Object -Property LastWriteTime | Select-Object -Last 1
$Imgfilename=$lastFile.Name
$filenameAndPath = $path + $Imgfilename
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
}
else {
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
}
OS is Win7, .NET version is 4.8
Any ideas about how to resolve this?
This works properly and sends email with attachments on machines with Win10, but not Win 7
PSeyler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.