Reputation: 408
I'm trying to use MailKit dll as assembly in Powershell but there is no way it works. I've tried with add-type and with [System.Reflection.Assembly] methods bwithout success. The link to mailkit library:
https://github.com/jstedfast/MailKit
With this method :
$path="$HOME\.nuget\packages\mailkit\1.16.1\lib\net451\MailKit.dll"
[System.Reflection.Assembly]::LoadFile($path)
ther isn't reference to the assembly in memory. With this method :
Add-Type -Path $path
this is the error:
- Add-Type -Path $path
- ~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
- FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
Thanks
Daniele
Upvotes: 1
Views: 4745
Reputation: 2139
this full script may help others:
# search for "Test" in subject and MoveTo Archive/2018
$packages = split-path -parent $MyInvocation.MyCommand.Definition
add-type -path (Join-Path $packages "MimeKit.dll") | Out-Null
add-type -path (Join-Path $packages "MailKit.dll") | Out-Null
#Server and Mailbox Definitions
$mailserver = "mail.corp.com"
$username = "[email protected]"
$password = "password"
$cnn = New-Object MailKit.Net.Imap.ImapClient
$cnn.Connect($mailserver)
$cnn.Authenticate($username,$password)
$cnn.Inbox.Open([MailKit.FolderAccess]::ReadWrite)
$query = [MailKit.Search.SearchQuery]::SubjectContains("Test")
#$orderBy = @([MailKit.Search.OrderBy]::Arrival)
#filter
$uids = $cnn.Inbox.Search($query) #$orderby) not working yet
#download
$msgs = $cnn.Inbox.Fetch($uids, [MailKit.MessageSummaryItems]::UniqueId -bor [Mailkit.MessageSummaryItems]::BodyStructure)
#do something
#move
$archive = $cnn.GetFolder("Archive.2018")
$cnn.Inbox.MoveTo($uids, $archive)
$cnn.Disconnect($true)
Upvotes: 4
Reputation: 408
I've found that MailKit had a reference to MimeKit dll, but there is no error loading MailKit.dll, so it's necessary to load MimeKit.dll also.
[System.Reflection.Assembly]::LoadFile("$home\.nuget\packages\MailKit\1.16.1\lib\net451\MailKit.dll")
[System.Reflection.Assembly]::LoadFile("$home\.nuget\packages\mimekit\1.16.1\lib\net451\MimeKit.dll")
Upvotes: 2
Reputation: 1723
Check the path. For me works just fine with the absolute path in $MailKitDllPath
:
Add-Type -Path $MailKitDllPath
$client = New-Object MailKit.Net.Smtp.SmtpClient
Upvotes: 2