在Powershell中将MailKit DLL加载为程序集

问题描述 投票:0回答:3

我试图在Powershell中使用MailKit dll作为程序集,但它没有办法。我尝试使用add-type和[System.Reflection.Assembly]方法,但没有成功。 mailkit库的链接:

https://github.com/jstedfast/MailKit

用这种方法:

 $path="$HOME\.nuget\packages\mailkit\1.16.1\lib\net451\MailKit.dll" 
  [System.Reflection.Assembly]::LoadFile($path)

在内存中没有引用程序集。用这种方法:

Add-Type -Path $path

这是错误:

  • Add-Type -Path $ path
  • ~~~~~~~~~~~~~~~~~~~~ CategoryInfo:NotSpecified:(:) [Add-Type],ReflectionTypeLoadException FullyQualifiedErrorId:System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

谢谢

丹尼尔

powershell .net-assembly mailkit
3个回答
1
投票

检查路径。对我来说,在$MailKitDllPath的绝对路径上工作得很好:

  Add-Type -Path $MailKitDllPath
  $client = New-Object MailKit.Net.Smtp.SmtpClient

0
投票

我发现MailKit有一个对MimeKit dll的引用,但加载MailKit.dll没有错误,因此也需要加载MimeKit.dll。

[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")

0
投票

这个完整的脚本可以帮助他人:

# 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)
© www.soinside.com 2019 - 2024. All rights reserved.