Powerhsell Send Email Using Outlook

From Michael's Information Zone
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}