23 Aug 2010

Send Outlook Email Via PowerShell

Just to complete the topic of sending emails via PowerShell, [Lies, I still have to blog about the Send-MailMessage cmdlet.] I thought I would include a quick look at sending emails from Outlook via PowerShell.  Again, this script could be replaced by using the PowerShell cmdlet, Send-MailMessage

The script below is based on a script submitted to TechNet by Kent Finkle:

   1:  param 
   2:  (        
   3:      [string]$email = $(read-host "Enter a recipient email"),
   4:      [string]$subject = $(read-host "Enter the subject header"), 
   5:      [string]$body = $(read-host "Enter the email text (optional)")
   6:  )   
   7:   
   8:  # Functions
   9:   
  10:  function Send-Email
  11:  (
  12:      [string]$recipientEmail = $(Throw "At least one recipient email is required!"), 
  13:      [string]$subject = $(Throw "An email subject header is required!"), 
  14:      [string]$body
  15:  )
  16:  {
  17:      $outlook = New-Object -comObject  Outlook.Application 
  18:      $mail = $outlook.CreateItem(0) 
  19:      $mail.Recipients.Add($recipientEmail) 
  20:      $mail.Subject = $subject 
  21:      $mail.Body = $body   # For HTML encoded emails 
  22:      # $mail.HTMLBody = "<HTML><HEAD>Text<B>BOLD</B>  <span style='color:#E36C0A'>Color Text</span></HEAD></HTML>"   
  23:      # To send an attachment 
  24:      # $mail.Attachments.Add("C:\Temp\Test.txt")    
  25:      $mail.Send() 
  26:      Write-Host "Email sent!"
  27:  }   
  28:   
  29:  # Main Script Body
  30:   
  31:  Write-Host "Starting Send-MailViaOutlook Script."   
  32:  # Send email using Outlook
  33:  Send-Email -recipientEmail $email -subject $subject -body $body     
  34:  Write-Host "Closing Send-MailViaOutlook Script."   
  35:   
  36:  # End of Script Body

Download the script from here.  If you have any comments, I would appreciate all feedback.  Thanks.

Also, just to note that the code for this post was highlighted (badly!) using the Neat Highlighter website. Unfortunately, the code snippet produced by the Neat Highlighter website was so poor that I couldn’t use it, and I have updated this post to use a snippet generated by the Insert Code for Windows Live Writer plugin. I also tried to use the Code Formatter Plugin, as recommended by Scott Hanselman, but it constantly froze Windows Live Writer. Also, I discovered how to use the <strike> HTML tag!

7 comments:

  1. Please, can you tell me how can I send an email to multiple recipient?
    I receive this error:
    Exception calling "Send" with "0" argument(s): "Outlook does not recognize one or more names. "

    In Main Script Body I added :
    $email = "name@domain;name2@domain"
    Send-Email -recipientEmail $email -subject $subject -body $body

    And in the function I put:
    $recipientEmail | ForEach-Object {$mail.Recipients.Add($_)} #instead of line 19

    Thanks a lot

    ReplyDelete
  2. Hi, I solved with:

    [String[]]$email #line 3

    and

    [String[]]$email = 'name@domain','name2@domain' #line 19

    Thanks the same. Bye.

    ReplyDelete
  3. Thank you very much for sharing this. you saved me having to use gmail.

    ReplyDelete
  4. is there any way to attach more than one file?

    ReplyDelete
  5. I figured that one out -- you just have to put
    $mail.Save()
    between each
    $mail.Attachments.Add()
    However I am still stuck on one item:
    I would very much like to send to a group which I have in my personal contacts.... please can you help me modify the script to send to a group?
    Many thanks

    ReplyDelete
  6. You send it Twice $mail.Send()
    and then Send-Email

    ReplyDelete
    Replies
    1. No he hasn't.

      $mail.Send() is only called when he calls his function Send-Email, which he only does once.

      You're confusing his function with the cmdlet Send-MailMessage.

      Delete