I'm attempting to use the new MailKit / Mimekit instead of Send-MailMessage in Powershell. I found Send-MailKitMessage 1.0.1 on the Powershell Gallery
My needs are simple and the Powershell will be used for internal alerts only, so I simplified the code to suit my needs.
***using namespace MailKit
using namespace MimeKit
$emailDLL = "F:\Apps\Email\MailKit.dll"
$mimeDll = "F:\Apps\Email\MimeKit.dll"
Add-Type -Path $emailDLL
[System.Reflection.Assembly]::LoadFile($mimeDll)
#extend classes to be available to the calling script (MimeKit assembly is loaded first from the manifest file so is available when this module loads) class MailboxAddressExtended : MailboxAddress { #does not allow parameterless construction MailboxAddressExtended([string]$Name, [string]$Address ) : base($Name, $Address) { [string]$Name, #can be null [string]$Address #cannot be null } }
class InternetAddressListExtended : InternetAddressList {}
$List = New-Object InternetAddressListExtended # of type MimeKit.InternetAddress
$smtpClient = New-Object MailKit.Net.Smtp.SmtpClient
$message = [MimeKit.MimeMessage]::new()
$TextPart = [MimeKit.TextPart]::new("html")
#endregion
[string[]]$recipients = "email@testdomain.com".Split(',').Trim()
$from = "Dev_Test@testdomain.com"
$smtpSvr = '190.132.148.22'
$smtpPort = 25
function sendEmail ($subjectline,$msgbody) {
$message.From.Add($from)
foreach ($address in $recipients) {$List.Add($address)}
$message.To.AddRange($List)
$TextPart.Text = $msgbody
$message.Body = $TextPart
$message.Subject = $subjectline
if(!$smtpClient.IsConnected) {
$smtpClient.Connect($smtpSvr)
}
if ($smtpClient.IsConnected) {
$smtpClient.Send($message)
$smtpClient.Disconnect($true)
}***
}
When I run the above code in a PS1 script, I get an error:
Unable to find type [MailboxAddress]
I can manually enter the same code in a Powershell window and it works as expected. I have used PS5 v5.1, PS7 v7.1 and I get the same error. I went with the above code since it allowed me to send emails to more than one individual / group. While relatively new to Powershell, I was able to work through all the previous issues to come up with the above code.
I greatly appreciate any guidance that can be provided.