Skip to content

Instantly share code, notes, and snippets.

@chrdek
Created February 10, 2021 23:16
Show Gist options
  • Select an option

  • Save chrdek/be5549f969f6e46c767656011b2c1079 to your computer and use it in GitHub Desktop.

Select an option

Save chrdek/be5549f969f6e46c767656011b2c1079 to your computer and use it in GitHub Desktop.
Create save a new draft email message with attachment using powershell
<#
Create a new email -with attachment- in outlook and save draft.
#>
$OL = New-Object -ComObject Outlook.Application
Start-Sleep 5
#Create Mailbox Item
$mItem = $OL.CreateItem(0);
#Add Messages etc..
[string]$bodyMsgPart1 += $([Int[]]@(13,10,913,961,967,953,954,972,32) | %{$_ -as [char]});
[string]$bodyMsgPart2 += $([Int[]]@(956,942,957,965,956,945,44,13) | %{$_ -as [char]});
[string]$bodyMsgPart3 += $([Int[]]@(10,13,10,924,942,957,965,956,945,32,49,32) | %{$_ -as [char]});
[string]$bodyMsgPart4 += $([Int[]]@(948,949,973,964,949,961,959,32) | %{$_ -as [char]});
[string]$bodyMsgPart5 += $([Int[]]@(956,942,957,965,956,945,46) | %{$_ -as [char]});
$bodyMsg = @($($bodyMsgPart1 -replace " ",""),
$($bodyMsgPart2 -replace " ",""),
$($bodyMsgPart3 -replace " ",""),
$($bodyMsgPart4 -replace " ",""),
$($bodyMsgPart5 -replace " ","")) -join ' ';
$mItem.To = "email@testdomain.con"
$mItem.Subject = $("FileAttachment_Date_$([System.DateTime]::UtcNow.Day)_$([System.DateTime]::UtcNow.ToLongDateString().Split(' ')[2])").toUpper();
$mItem.Body = $bodyMsg;
[string] $dirPath = "$($env:USERPROFILE)\Documents\FileSample_Date_2021.xlsx"
#Add relevant attachments to messages.
$mItem.Attachments.Add($dirPath);
$mItem.Display(); $mItem.Save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment