Thursday, December 8, 2016

PowerShell - Send Test Email (Office 365) Using PowerShell

 

Here is a sample PowerShell script which can be to test email communication using a SMTP server. In this script, I am using Office 365 SMTP server, smtp.office365.com.

 

 

 

Script:

 

#

#

$SMTPServer = "smtp.office365.com"

$EmailFrom = "Santhosh@virtualsecuritysolutions.com"

$EmailTo = "santhosh@ss-ts.com"

#

#Send-MailMessage Reference - https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/send-mailmessage

#

#

 

Write-Host "`t`tSelect 1 - SMTP Test Message with No Attachmnet" -ForegroundColor Red

Write-Host "`t`tSelect 2 - SMTP Test Message" -ForegroundColor Red

$Option = Read-Host

#

#

#

Function EmailTest_No_Attachment

{

#with no attachement

#

$Cred = Get-Credential

$Sub = "SMTP Test Message - 1 - No Attachmnet"

$Bmessage = "SMTP Test Message - 1 with Attachment"

Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Sub -Body $Bmessage -SmtpServer $SMTPServer -Credential $cred -UseSsl -Port 587

}

 

Function EmailTest_With_Attachment

{

#with Attachment

#

$Cred = Get-Credential

$Sub = "SMTP Test Message - 1 with Attachment"

$Bmessage = "Test email body message - With Attachment"

$MyAttachment =  "C:\temp\1.docx"

Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Sub -Body $Bmessage -Attachments $MyAttachment -SmtpServer $SMTPServer -Credential $cred -UseSsl -Port 587

}

 

#

#

 

Switch ($Option)

    {

        1 {EmailTest_No_Attachment}

        2 {EmailTest_With_Attachment}

    default {"Invalid Selection"}

    }

 

Script download options:

1.        OneDrive - https://1drv.ms/t/s!AuVEEHIwTxv9h4ZXLslcu1MzA8ZSqw

2.       TechNet Gallery - https://1drv.ms/t/s!AuVEEHIwTxv9h4ZXLslcu1MzA8ZSqw

 

 

0 comments:

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More