【问题标题】:Loading MailKit DLL as Assembly in Powershell在 Powershell 中将 MailKit DLL 作为程序集加载
【发布时间】:2017-11-19 02:30:13
【问题描述】:

我正在尝试在 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)

ther 不是对内存中的程序集的引用。 用这个方法:

Add-Type -Path $path

这是错误:

  • 添加类型-路径 $path
  • ~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
    • FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

谢谢

丹尼尔

【问题讨论】:

  • 发布您正在使用的实际代码、错误消息以及您如何安装 DLL。这里的人无法猜测你做了什么/正在做什么。

标签: powershell .net-assembly mailkit


【解决方案1】:

检查路径。对我来说,使用$MailKitDllPath 中的绝对路径就可以了:

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

【讨论】:

    【解决方案2】:

    我发现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")
    

    【讨论】:

    • 起初,我尝试使用 '[System.Reflection.Assembly]::LoadFile()' 加载所有依赖项。但是使用 Add-Type 它可以在不加载任何内容的情况下工作。
    • 这是我的 $PSVersionTable : PSVersion 5.1.15063.413 PSEdition 桌面
    • Add-Path 对我不起作用,但上面的方法起作用了,我还需要加载 MimeKit.dll 来创建一个新的 SMTPClient 对象。
    【解决方案3】:

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

    # 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@corp.com"
    $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)
    

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2018-03-24
      • 1970-01-01
      • 2010-11-18
      相关资源
      最近更新 更多