header image

Outlook: sending emails

Posted by: | August 8, 2011 | No Comment |

We’ve looked at examining the email folders and their contents, as well as cleaning out old emails.

Its time to look at sending emails. This function shows the skeleton of the process

function send-mailitem {            
param (            
            
 [string]$to,            
             
 [string]$from,            
             
 [string]$subject,            
             
 [string]$body            
            
)            
$outlook = New-Object -ComObject Outlook.Application            
            
$email = $outlook.CreateItem($types::olMailItem)            
            
$email.To = $to            
$email.subject = $subject            
$email.body = $body            
            
$email.SendUsingAccount = $outlook.Session.Accounts | where {$_.DisplayName -eq $from}            
$email.Send()            
}

Create a new mail item object. Set the To, subject and body

The choose the account from which to send the email – remember I have multiple hotmail accounts connected through Outlook. If you are using Exchange you may want to test using smtpaddress instead of DisplayName

We can then use the send method to fire off the email.

There are a number of things to do to tidy this up:

  1. Create a function to get the account
  2. Accept the To parameter from the pipeline to enable multiple recipients
  3. Check the email addresses using regex (oh joy)
under: Office 2010, Outlook