This is a sample script that shows how to send an email from PowerShell without installing the SMTP service on the local system. There is a VBScript example similar to this in the Technet script repository, but it wasn't too hard to convert it to Powershell.
$email = new-object -comobject "cdo.message"
$email.From = "sample@domain.com"
$email.To = "sample@domain.com"
$email.Subject = "App Errors"
$email.TextBody = "The app encountered the following errors: `n`n"
foreach ($f in get-content app.log | select-string "does not exist")
{
$email.TextBody += "$f `n"
}
$email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.sample.com"
$email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
$email.Configuration.Fields.Update()
$email.Send()
2 comments:
I think it would be more robust to use the SMTP support directly in .NET. See System.Net.Mail.
Hi,
if i try to email using CDO message with powershell - i get the following message:
Exception calling "Send" with "0" argument(s): "The message could not be sent to the SMTP server. The transport error c
ode was 0x800ccc15. The server response was not available
"
At D:\misc_scripts\testmail.ps1:24 char:12
+ $email.Send <<<< ()
I dont want to allow powershell.exe through McAfee - else any powershell scripts downloaded from web can be executed - which will mess up production servers.
What can i include in the Mcafee - will adding cdosys.dll to exclude list work???
Post a Comment