Skip to main content

Send with Sonic SMTP

# Define the SMTP server details
$smtpServer = "smtp.labnet.local"
$smtpPort = 25

# Define the email details
$to = "[email protected]"
$from = "[email protected]"  # Replace with your email address
$subject = "Test Email"
$body = "This is a test email sent via PowerShell."

# Create the SMTP client
$smtp = New-Object System.Net.Mail.SmtpClient($smtpServer, $smtpPort)

# Set the SMTP client credentials if required
# $smtp.Credentials = New-Object System.Net.NetworkCredential("username", "password")

# Create the mail message
$mail = New-Object System.Net.Mail.MailMessage
$mail.From = $from
$mail.To.Add($to)
$mail.Subject = $subject
$mail.Body = $body

# Send the email
try {
    $smtp.Send($mail)
    Write-Host "Email sent successfully."
} catch {
    Write-Error "An error occurred: $_"
}

# Clean up
$mail.Dispose()