powershell email

  • Hi,

    I am trying to send email using powershell. Here is the code i am using:

    $emailFrom = "user1@abc.com"

    $emailTo = "user2@abc.com"

    $subject = "Sending a File " + [System.DateTime]::Now.ToString()

    $body = "I'm sending a file! - Acknowledge on receiving the mail"

    $file = "D:\List.txt"

    $smtpServer = "xx.xx.xx.xxx"

    $smtp = new-object System.Net.Mail.SmtpClient($smtpServer)

    $msg = new-object System.Net.Mail.MailMessage

    $msg.From = $emailFrom

    $msg.To = $emailTo

    $msg.Subject = $subject

    $msg.body = $body

    $msg = new-object System.Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body)

    [System.Net.Mail.Attachment] $attachment = new-object System.Net.Mail.Attachment($file)

    $msg.Attachments.Add($attachment)

    #$smtp.Credentials = [System.Net.CredentialCache]:: DefaultNetworkCredentials

    #$smtp.UseDefaultCredentials = $true

    $smtp.Send($msg)

    If i try to execute in powershell console i get the following error message:

    "To" is a ReadOnly property.

    At D:\mailtest.ps1:13 char:6

    + $msg. <<<< To = $emailTo

    Exception calling "Send" with "1" argument(s): "Failure sending mail."

    At D:\mailtest.ps1:24 char:11

    + $smtp.Send <<<< ($msg)

    Also in McAfee - it shows in the log:

    Blocked by port blocking rule C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exeAnti-virus Standard Protection: Prevent mass mailing worms from sending mail

    How do i get this to work. I dont want to include powershell.exe in McAfee exclude list as then any ps scripts will be executed against the server.

    Can i use CDO.Message with powershell - if so what is the syntax for that. Also in this case i dont want to include cscript.exe as a part in McAfee - instead can i include CDOsys.dll ??

    Please let me know.

    Thanks,

    Natalie

  • Your Anti-virus software will have a problem with anything that uses port 25 unless you change the anti-virus software.

    Assuming you're managing McAfee with ePO, is there a reason not to use policy and folders to isolate that server and apply an exclusion? Also, those particular exclusion lists are typically separated into Workstation and Server settings which limits the scope if that's the concern.

    Other options which probably aren't worth considering for one reason or another:

    1) Rename the powershell executable and add that to the McAfee exclusion list.

    2) Configure an SMTP server on the machine you want to send from. Create the message and copy to the drop folder for delivery.

    3) Change your SMTP server to use a non-default port number.

    4) Remove McAfee.

  • Hi,

    Will try out your suggestions.

    Thanks for your reply.

    Natalie.

  • This is what I've been using to send email via Powershell:

    $sender = "SQLAgent@FooServer.MyCompany.com"

    $recipient = "24x7DBA@MyCompany.com"

    $server = "MYSMTP.SERVER"

    $file = "Foo.txt"

    $subject = "Sending Foo.txt at " + [System.DateTime]::Now

    $body = "Foo Report"

    $msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body

    $attachment = new-object System.Net.Mail.Attachment $file

    $msg.Attachments.Add($attachment)

    $client = new-object System.Net.Mail.SmtpClient $server

    $client.Send($msg)

    Simple and straightforward, but not very explicit either...one of these days I'll tidy it up.

    Your friendly High-Tech Janitor... 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply