Search code examples
powershellautomationemail-attachmentsoutlook-object-model

Mail Automation Task using Powershell


I had been trying to automate the process of sending mails along with attachments via my outlook account using Powershell.

$OL = New-Object -ComObject outlook.application

Start-Sleep 5

<#
olAppointmentItem
olContactItem
olDistributionListItem
olJournalItem
olMailItem
olNoteItem
olPostItem
olTaskItem
#>

#Create Item
$mItem = $OL.CreateItem("olMailItem")

$mItem.To = "[email protected]"
$mItem.Subject = "PowerMail"
$mItem.Body = "SENT FROM POWERSHELL"

$mItem.Send()

Stuck with adding attachments in the code. Please help me out.


Solution

  • Look at the documentation here.

    In your case something like:

    $mItem.Attachments.Add( "C:\My Documents\Q496.xls", olByValue, 1, "4th Quarter 1996 Results Chart"
    

    Arguments are Source, Type, Position & DisplayName.