Test email from SharePoint using PowerShell


The following PowerShell script was written for my post on configuring TLS between SharePoint and Exchange. However, since it was buried in process, I wanted to create a separate post just sharing the script, because it will be easier to maintain and use separately when needed.

Why use a script anyway?

I find this script very useful when testing mail flow from Sharepoint since it uses the “SPUtility::SendEmail” API , sends mail, captures the correct logs and presents them by launching notepad, all from a single server.

The Script:

# Ensure Microsoft.SharePoint.PowerShell is loaded 
 Add-PSSnapin Microsoft.SharePoint.Powershell -ea 0

 While ($web -eq $null){
 $web = Get-SPWeb (Read-Host "Input SPWeb URL using http://")

$email = (Read-Host "Input E-mail recipient")
$subject = (Read-Host "Input E-mail Subject")
$body = (Read-Host "Input E-mail Body")

#specify start time of action
 $StartTime = (Get-Date).AddMinutes(-1).ToString()

# Try sending e-mail via SharePoint.
 $send = [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,0,0,$email,$subject,$body)

#what to do if it fails
 if ($send -eq $false -and $web -ne $null){
 write-host "It didn't work, checking ULS for errors. Please stand by..." -foregroundcolor Red -backgroundcolor Yellow

#specify end time of action
 $EndTime = (Get-Date).AddMinutes(+1).ToString()

#make dir if it does not exist
 $TARGETDIR = "c:\logs"
 if(!(Test-Path -Path c:\logs)){
 New-Item -ItemType directory -Path $TARGETDIR

#finding error and creating log
 start-sleep 5
 Get-SPLogEvent -StartTime $StartTime -EndTime $EndTime | Where-Object {$_.Category -eq "E-Mail"} | Export-Csv -LiteralPath "$TARGETDIR\log.csv"

#starting notepad to open log
 start notepad.exe "$TARGETDIR\log.csv"

#what to do if it works
 if ($send -eq $true -and $web -ne $null){
 write-host "It Worked..Congrats!" -foregroundcolor DarkGreen -backgroundcolor White



As you can see below the script will ask for input and you will specify the SPWeb url, E-Mail recipient, E-Mail Subject and E-Mail Body. If you enter the SP Web url incorrectly, it will keep asking. Also, if the e-mail is not sent, you will be notified on screen and NOTEPAD will pop-up with the associated ULS logs.

I hope you find this useful and thanks for reading!



  1. Hello

    I’ve got a problem with my Shrapoint 2019 (on premise), and it’s outgoing mail function.
    I need SP2019 site, sends mail to a Zimbra-MTA that requires authentication and TLS encryption, and expecting smtp connection via port:587. So I put these parameters in Sharepoint Central Administration (in Outgoing Mail config page). I also set the application credential key stuff. And I have used your script.
    But that is the main problem I got, in the log.csv:
    “The remote certificate is invalid according to the validation procedure”
    So I imported the self-signed CA certificate into Trusted Root Certification Authorities, on the Sharepoint Server. But I continue to receive that error, all the time.

    Could you give me a clue?

    Thanks in advance.


    1. It’s definitely a cert issue. Make sure you install the cert of the SMTP server in the trusted root authority on each sp server. Also check out CAPI2 logging.. this may help.


Leave a Reply to Mike Lee Cancel reply