Send email with PowerShell

Ανοίγουμε και εκτελούμε powershell as administrator

Για την εγκατάσταση του PowerShell 7 πάμε και κατεβάζουμε το msi από εδώ
ή δίνουμε και 
winget install Microsoft.PowerShell

Ορισμός πολιτικής εκτέλεσης του Windows PowerShell

Set-ExecutionPolicy RemoteSigned

Εγκατάσταση PowerShellGet module

Install-Module PowershellGet -Force 
ή
Install-Module -Name PowerShellGet -AllowPrerelease -Force

Εγκατάσταση Send-MailKitMessage module

Install-Module -Name Send-MailKitMessage -Force

Δημιουργούμε ενα ps script και βάζουμε μέσα το παρακάτω

# Use secure connection if available ([bool], optional)
$UseSecureConnectionIfAvailable = $true

# Authentication ([System.Management.Automation.PSCredential], optional)
$Credential = [System.Management.Automation.PSCredential]::new("YOUR_FROM_SMTP ADDRESS", (Read-Host -Prompt "Enter password" -AsSecureString))

# SMTP server ([string], required)
$SMTPServer = "smtp.thirdparty.com"

# Port ([int], required)
$Port = "587"

# Sender ([MimeKit.MailboxAddress] http://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"YOUR_FROM_SMTP ADDRESS"

# Recipient list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"YOUR_TO ADDRESS")

# Subject ([string], optional)
$Subject = [string]"This is the subject"

# Text body ([string], optional)
$TextBody = [string]"This is the text body"

# Splat parameters
$Parameters = @{
    "UseSecureConnectionIfAvailable" = $UseSecureConnectionIfAvailable
    "SMTPServer"                     = $SMTPServer
    "Credential"                     = $Credential
    "Port"                           = $Port
    "From"                           = $From
    "RecipientList"                  = $RecipientList
    "Subject"                        = $Subject
    "TextBody"                       = $TextBody
}

# Send message
Send-MailKitMessage @Parameters


Πάμε στο path που το αποθηκεύσαμε και το εκτελούμε
 
Tag: send emails via Microsoft 365 using the Send-MailKitMessage PowerShell cmdlet