I would like to send an email via PowerShell using nuget package called MailKit. After downloading it along with all of the package dependencies (I managed to go over the loop dependency issues) I have created a script which I hoped will send the email I wanted:
Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\MimeKit.2.15.1\lib\net50\MimeKit.dll"
Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\MailKit.2.15.0\lib\net50\MailKit.dll"
$SMTP = New-Object MailKit.Net.Smtp.SmtpClient
$Message = New-Object MimeKit.MimeMessage
$TextPart = [MimeKit.TextPart]::new("plain")
$TextPart.Text = "MailKit test message"
$Message.From.Add("user1@domain.com")
$Message.To.Add("user2@domain.com")
$Message.Subject = 'Test'
$Message.Body = $TextPart
$SMTP.Connect('172.xx.xx.x', 25, $False)
$SMTP.Send($Message)
$SMTP.Disconnect($true)
$SMTP.Dispose()
Once I execute the above it fails with
MethodInvocationException: Exception calling "Send" with "1" argument(s): "relay not permitted"
The SMTP server is in-house, no firewall in between and most importantly no user/password required for the authentication. PowerShell version I use is 7.1.4
I know that the SMTP server works just fine, as I actively use it with some 3rd party apps which simply worked out of the box once I pointed then to the correct IP and port.