Random_scripts/Test-SMTPSend-Auth-M365.ps1

65 lines
2.0 KiB
PowerShell

<#
.SYNOPSIS
Test script to verify SMTP (Microsoft 365) connectivity using an app password.
.DESCRIPTION
This script attempts to send a test message using Authenticated SMTP with Microsoft 365.
Useful for diagnosing SMTP authentication issues with accounts that have MFA enabled.
.AUTHOR
Generic / Public version (original author removed for portability)
.NOTES
This script is intended for test purposes and should not be used in production automation.
# CHANGELOG
--------------------------------------------------------------------------------
Date | Version | Author | Description
------------|---------|---------------|------------------------------------------
2025-07-18 | 1.0.0 | Public/GPT | Initial version - generic SMTP test script
--------------------------------------------------------------------------------
#>
Clear-Host
# SMTP settings
$smtpServer = "smtp.office365.com"
$smtpPort = 587
$from = "youruser@yourdomain.com"
$to = "youruser@yourdomain.com"
$subject = "SMTP Test"
$body = "This is a test message sent via smtp.office365.com with an app password."
# Secure password prompt
$securePassword = Read-Host "Enter your app password" -AsSecureString
$credential = New-Object System.Management.Automation.PSCredential($from, $securePassword)
# Create email
$mail = New-Object system.net.mail.mailmessage
$mail.From = $from
$mail.To.Add($to)
$mail.Subject = $subject
$mail.Body = $body
$mail.IsBodyHtml = $false
# Create SMTP client
$smtp = New-Object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.EnableSsl = $true
$smtp.Credentials = $credential
$smtp.Timeout = 10000
Write-Host "⏳ Sending mail via ${smtpServer}:${smtpPort} as ${from}..."
try {
$smtp.Send($mail)
Write-Host "`n✅ Message sent successfully to $to"
}
catch {
Write-Host "`n❌ Failed to send message:"
Write-Host $_.Exception.Message
if ($_.Exception.InnerException) {
Write-Host "`nDetails:"
Write-Host $_.Exception.InnerException.Message
}
}