| 437-991-3573 | Data Engineering Services
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance

Troubleshooting Azure VM SQL Server Database Mail using SmtpClient class, PowerShell and SendGrid
by BF (Principal Consultant; Architecture; Engineering)

Check if the Server can send email via Powershell. Good for troubleshooting Database Mail to rule out the Server can send mail successfully or not.

Powershell Code:

# Create From/To Addresses:

$from = New-Object System.Net.Mail.MailAddress ""
$to = New-Object System.Net.Mail.MailAddress ""

# Create Message:
$message = new-object System.Net.Mail.MailMessage $from, $to
$message.Subject = "Using the SmtpClient class and PowerShell"
$message.Body = "This is a test mail from Powershell."

# Set SMTP Server and create SMTP Client:
$server = ""
$port = 25
$client = new-object ($server,$port)

# Specify the authentication:
$credential = Get-Credential
$client.UseDefaultCredentials = $false
$client.EnableSsl = $true;
$client.Credentials = new-object System.Net.NetworkCredential($credential.UserName,$credential.Password)

# Send the message:
" "
"Sending an e-mail message to {0} by using SMTP host {1} port {2}." -f $to.ToString(), $client.Host, $client.Port
" "

Related: SQL Server Database Mail - Success, Failure, Logs, Config