Tests SMTP AUTH sending via any Exchange Online mailbox that has SmtpClientAuthenticationDisabled enabled. Just enter the password when prompted and it runs.
# 1) Ensure TLS 1.2 (required for STARTTLS with Microsoft 365)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# 2) Sender / Recipient
$From = "mailbox@yourdomain.com"
$To = "recipient@yourdomain.com"
# 3) SMTP Settings
$SmtpServer = "smtp.office365.com"
$Port = 587
# 4) Preflight: TCP check
Write-Host ("Preflight: Checking TCP connectivity to {0}:{1} ..." -f $SmtpServer, $Port)
$tcp = Test-NetConnection -ComputerName $SmtpServer -Port $Port
if (-not $tcp.TcpTestSucceeded) {
throw ("No connection to {0}:{1}. Check firewall/proxy/ISP." -f $SmtpServer, $Port)
}
# 5) Securely prompt for password and build PSCredential (username taken from $From)
$SecurePwd = Read-Host -AsSecureString "Password for $From (not displayed)"
$Cred = [pscredential]::new($From, $SecurePwd)
# -- Debug check: should display 'mailbox@yourdomain.com' --
# Write-Host "Cred.UserName = $($Cred.UserName)"
# 6) Build test email
$subject = "SMTP AUTH Test from $From - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
$body = @"
This is a test mail via SMTP AUTH (Port $Port / STARTTLS) from $From.
Client: $env:COMPUTERNAME
Timestamp: $(Get-Date -Format o)
"@
$mail = [System.Net.Mail.MailMessage]::new($From, $To, $subject, $body)
# 7) Send with STARTTLS + Auth
$smtp = [System.Net.Mail.SmtpClient]::new($SmtpServer, $Port)
$smtp.EnableSsl = $true # STARTTLS on 587
$smtp.UseDefaultCredentials = $false
$smtp.Credentials = $Cred.GetNetworkCredential() # Pass credentials securely
$smtp.DeliveryMethod = [System.Net.Mail.SmtpDeliveryMethod]::Network
$smtp.Timeout = 100000
try {
$smtp.Send($mail)
Write-Host "✅ Mail sent to $To. Also check 'Sent Items' of $From." -ForegroundColor Green
}
catch [System.Net.Mail.SmtpException] {
Write-Error ("❌ SMTP send failed: {0} (StatusCode: {1})" -f $_.Exception.Message, $_.Exception.StatusCode)
if ($_.Exception.StatusCode -eq [System.Net.Mail.SmtpStatusCode]::MustIssueStartTlsFirst) {
Write-Warning "STARTTLS/TLS 1.2 handshake failed. Check proxy/SSL inspection/TLS policies."
}
}
if ($mail) { $mail.Dispose() }
if ($smtp) { $smtp.Dispose() }
H@ppy H@cking