Forum Discussion

Alan2022's avatar
Alan2022
Iron Contributor
May 30, 2022

Using Outlook Application to Email.

Sharing Mode.

When SMTP Port is block by your firewall.

 

 

# create COM object named Outlook
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
Add-Type -assembly "System.Runtime.Interopservices"
try
{
    $Outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
    # create Outlook MailItem named Mail using CreateItem() method
	$Mail = $Outlook.CreateItem(0)

	# add properties as desired
	$Mail.To = "<email address removed for privacy reasons>; <email address removed for privacy reasons>"
	$Mail.Subject = "Subject" 
	$Mail.Body = "Email Sent using PowerShell via Outlook

	Best Regards,  
	<Name>"

	# Add Attachement
	$Mail.Attachments.Add("$($env:USERPROFILE)\Desktop\File.txt");

	# Send message
	$Mail.Send()

	# Give time to send the email
	Start-Sleep 5

	#quit and cleanup
	$Outlook.Quit() 
	[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
}
catch
{
    write-host "Sending Email Failed!"
}

 

 

 

No RepliesBe the first to reply

Share