Users: 15,000+ | Page Views: 50,000+ | PPS: 2.5
TechDevOps.com
Explore Tools
{Experts in Microsoft SQL Server Operations, Performance, HA/DR, Data Migrations & Cloud Computing}



Troubleshooting Azure VM SQL Server Database Mail using SmtpClient class, PowerShell and SendGrid
by BF (Senior MSSQL Operations - Enterprise, Cloud, Strategy)
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