Add-Type error MailKit

Moore, Peter 0 Reputation points
2023-03-21T19:21:38.5466667+00:00

I am trying to write a Powershell script that runs MailKit.3.6.0 starting with the command:

Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\MailKit.3.6.0\lib\netstandard2.0\MailKit.dll"

and get the error Add-Type : Unable to load one or more requested types.

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,058 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 43,931 Reputation points
    2023-03-22T13:28:48.1633333+00:00

    Hello there,

    Try adding some logging to the CharsetUtils static .ctor to see if that helps narrow things down.

    That class basically tries to load a bunch of widely-used text encodings and maps various aliases to them that are commonly used in mail headers. It tries to catch exceptions, but apparently some exceptions make it out.

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

  2. Rich Matheisen 44,776 Reputation points
    2023-03-22T14:41:59.1733333+00:00

    My guess is that it's failing to load MimeKit. MimeKit may be failing to load because of its own dependencies, so if MailKit fails to load and the LoaderExceptions points at MimeKit, try loading MimeKit and see why that's failing. Maybe it's BouncyCastle.Crypto that's missing.

    try{
        Add-type -path "C:\Program Files\PackageManagement\NuGet\Packages\MailKit.3.6.0\lib\netstandard2.0\mailkit.dll"}
    catch{
        $_.Exception.LoaderExceptions 
    }
    
    0 comments No comments