# send-mailkit.ps1
Import-Module Send-MailKitMessage

# load credentials from credentials.env
# only necessary if SMTP_XXX are not defined as env. variables
Get-Content credentials.env | ForEach-Object {
    if ($_ -match '^\s*([^#][^=]+)=(.+)$') {
        [System.Environment]::SetEnvironmentVariable(
            $Matches[1].Trim(), $Matches[2].Trim())
    }
}

# credential object 
$credential = [PSCredential]::new(
    $env:SMTP_USER,
    (ConvertTo-SecureString $env:SMTP_PASS -AsPlainText -Force))

# recipient list (MimeKit types)
$recipientList = [MimeKit.InternetAddressList]::new()
$recipientList.Add(
    [MimeKit.MailboxAddress]::Parse("alice@example.com"))

# combine credentials, recipient and other data
$params = @{
    SMTPServer    = $env:SMTP_HOST
    Port          = 587
    Credential    = $credential
    UseSecureConnectionIfAvailable = $true  # STARTTLS
    From          = [MimeKit.MailboxAddress]::Parse($env:SMTP_USER)
    RecipientList = $recipientList
    Subject       = "Hello from Send-MailKitMessage"
    TextBody      = "Sent using the MailKit module."
}

Send-MailKitMessage @params
Write-Host "Sent."
