PowerShell script to test SharePoint Send Mail

Thought it would be good to share some of my script to the public

Copy the below script and save as .ps1 file. Run it in SharePoint Management Shell with Admin Rights.

[sourcecode type=”powershell”]

$webUrl = Read-Host "Enter SharePoint Web Url, e.g. https://sharepoint.com"

$web = Get-SPWeb $webUrl;

if($web)
{
$header = New-Object System.Collections.Specialized.StringDictionary
$to = Read-Host "Enter Email TO address (e.g. abc@def.com) "
$header.Add("To",$to);
$header.Add("From","ahcheng@ahcheng.com")
$subject = Read-Host "Enter Email Subject "
$header.Add("Subject",$subject);
$header.Add("content-type","text/html");

$body = Read-Host "Enter Email Body Content "
$sent = [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,$header,$body);
if($sent)
{
Write-Host -f Green "Email ($to) Sent successfully"
}
else
{
Write-Host -f Red "Email failed to send"
}
}

[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *