info@techdevops.com | 437-991-3573 | Data Engineering Services
TechDevOps.com
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)
2017-12-02








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 "xyz@azure.com"
$to = New-Object System.Net.Mail.MailAddress "xyz@world.net"

# 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 = "smtp.sendgrid.net"
$port = 25
$client = new-object system.net.mail.smtpclient ($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
$client.Send($message)
" "


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