Add Test-SMTPRelay-M365.ps1

This commit is contained in:
Ivo Oskamp 2025-07-18 12:09:28 +02:00
parent 02ede18d6b
commit f5ea8371b2

60
Test-SMTPRelay-M365.ps1 Normal file
View File

@ -0,0 +1,60 @@
<#
.SYNOPSIS
Test unauthenticated SMTP relay via Microsoft 365 using MX record and IP-based connector.
.DESCRIPTION
This script sends a test email through Microsoft 365 (Exchange Online) using a connector
that allows mail relay based on the sender's IP address. It does not require authentication
and is useful for testing printers, scanners, or any other device that cannot use modern SMTP auth.
.AUTHOR
Generic / Public version (sanitized for open-source use)
.NOTES
Requires that your public IP is allowed in an Exchange Online connector.
Use the MX hostname (e.g. yourdomain-nl.mail.protection.outlook.com) as the SMTP endpoint.
# CHANGELOG
--------------------------------------------------------------------------------
Date | Version | Author | Description
------------|---------|----------------|------------------------------------------
2025-07-18 | 1.0.0 | Public / GPT | Initial release for generic SMTP relay test
--------------------------------------------------------------------------------
#>
# Variables (edit to match your domain and connector setup)
$from = "relay@yourdomain.com"
$to = "relay@yourdomain.com"
$smtpServer = "yourdomain-nl.mail.protection.outlook.com" # Replace with your actual MX record
$smtpPort = 25
$subject = "SMTP Relay Test via Microsoft 365 Connector"
$body = "This message was sent without authentication using an IP-allowed connector."
# Create email message
$message = New-Object system.net.mail.mailmessage
$message.From = $from
$message.To.Add($to)
$message.Subject = $subject
$message.Body = $body
$message.IsBodyHtml = $false
# Configure SMTP client (unauthenticated, no TLS)
$smtp = New-Object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.EnableSsl = $false
$smtp.DeliveryMethod = "Network"
$smtp.UseDefaultCredentials = $true
Write-Host "⏳ Attempting SMTP relay via $smtpServer..."
try {
$smtp.Send($message)
Write-Host "`n✅ Message successfully sent via connector."
}
catch {
Write-Host "`n❌ Failed to send mail:"
Write-Host $_.Exception.Message
if ($_.Exception.InnerException) {
Write-Host "`nDetails:"
Write-Host $_.Exception.InnerException.Message
}
}