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.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,766 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 47,901 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.