Using Outlook Application to Email.

Contributor

Sharing Mode.

When SMTP Port is block by your firewall.

 

# Exit Outlook
Get-Process "*outlook*" | Stop-Process –force
Start-Sleep 5

# create COM object named Outlook
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
Add-Type -assembly "System.Runtime.Interopservices"
try
{
    $Outlook = New-Object -comobject Outlook.Application
    # create Outlook MailItem named Mail using CreateItem() method
	$Mail = $Outlook.CreateItem(0)

	# add properties as desired
	$Mail.To = "<emailadd01@gmail.com>; <emailadd02@gmail.com>"
	$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
	Start-Sleep 5
}
catch
{
    write-host "Sending Email Failed!"
}

# Exit Outlook
Get-Process "*outlook*" | Stop-Process –force
Start-Sleep 5

 

 

0 Replies