Powerhsell Send Email Using Outlook

From Michael's Information Zone
Revision as of 15:25, 6 November 2018 by Michael.mast (talk | contribs) (Created page with "==Purpose== Use Outlook to send email on behalf of user running the script, instead of using smtp. ==Example== <ref>https://blogit.create.pt/andresilva/2017/12/07/sending-an-e...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Purpose

Use Outlook to send email on behalf of user running the script, instead of using smtp.

Example

[1]

$file="$ENV:userprofile\Desktop\$samaccountname.csv"
try { $body | Export-Csv -LiteralPath $file -NoTypeInformation } catch {clear; echo "Failed to create file, let IT know this failed"; pause; exit}

$outlook = New-Object -ComObject Outlook.Application
$mail = $outlook.CreateItem(0)
$mail.subject = "New User $samaccountname"
$mail.body = "See attached for user properties"
$mail.to = "someone@somewhere.com"
$mail.attachments.add($file)
try {
$mail.send()

} catch {clear; echo "Failed to send email, let IT know this failed"; pause; exit}