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:
- Create a function to get the account
- Accept the To parameter from the pipeline to enable multiple recipients
- Check the email addresses using regex (oh joy)