param( [string]$ConfigPath = (Join-Path (Split-Path -Parent $PSScriptRoot) 'config.ini'), [string]$PickupDirectory, [switch]$Send ) $ErrorActionPreference = 'Stop' . (Join-Path $PSScriptRoot 'mail-common.ps1') if ([bool]$Send -eq [bool]$PickupDirectory) { throw 'Choose exactly one mode: -Send or -PickupDirectory' } $context = Get-MailContext -ConfigPath $ConfigPath $settings = $context.Settings foreach ($name in @('SMTP_HOST', 'SMTP_PORT', 'SMTP_USER', 'SMTP_FROM', 'SMTP_REPLY_TO', 'SMTP_TEST_TO')) { if (-not $settings.ContainsKey($name) -or -not $settings[$name]) { throw "Missing setting: $name" } } if ($settings['SMTP_TLS'] -ne 'on' -or $settings['SMTP_STARTTLS'] -ne 'on') { throw 'SMTP_TLS and SMTP_STARTTLS must both be on' } $port = 0 if (-not [int]::TryParse($settings['SMTP_PORT'], [ref]$port) -or $port -lt 1 -or $port -gt 65535) { throw 'Invalid SMTP_PORT' } $testTo = New-Object System.Net.Mail.MailAddress($settings['SMTP_TEST_TO']) if ($testTo.Address -ne $settings['SMTP_TEST_TO']) { throw 'SMTP_TEST_TO must be a single bare email address' } $client = $null $message = $null $password = $null $imageStream = $null try { Add-Type -AssemblyName System.Drawing $imageStream = New-Object System.IO.MemoryStream $bitmap = New-Object System.Drawing.Bitmap(1, 1) try { $bitmap.SetPixel(0, 0, [System.Drawing.Color]::DarkBlue) $bitmap.Save($imageStream, [System.Drawing.Imaging.ImageFormat]::Png) } finally { $bitmap.Dispose() } $imageStream.Position = 0 $message = New-Object System.Net.Mail.MailMessage $message.From = New-Object System.Net.Mail.MailAddress($settings['SMTP_FROM']) $null = $message.ReplyToList.Add($settings['SMTP_REPLY_TO']) $null = $message.To.Add($testTo) $message.Subject = 'MassMailer internal SMTP transport test' $message.SubjectEncoding = [System.Text.Encoding]::UTF8 $html = '
Internal transport test. No marketing campaign was sent.
' + '