title: New-Object : Exception calling ".ctor" with "0" argument(s): "The type initializer for 'MimeKit.ParserOptions' threw an exception."
body: <p>Background:</p>
  <p>Trying to send an email with Powershell on Win10, and wanting to communicate with an SMTP server using port 587 and TLS 1.2.</p>
  <p>Trying to use the Nuget Gallery packages: <a href="https://www.nuget.org/packages/MailKit/3.3.0" rel="nofollow noreferrer">MailKit</a> and its dependency <a href="https://www.nuget.org/packages/MimeKit/" rel="nofollow noreferrer">MimeKit</a> because Microsoft does not recommend System.Net.Mail.SmtpClient given security concerns.</p>
  <p>Used Powershell Install-Package cmdlet to install the MailKit (this seemed to work) and then same for the MimeKit, but the MimeKit package installation failed (and as you can see the exception string is not particularly helpful as to the exact nature of the dependency loop):</p>
  <pre><code>PS C:\bin\ps1\renew_certs&gt; Install-Package -Name 'MimeKit' -Source &quot;https://www.nuget.org/api/v2&quot; -Force
  Install-Package : Dependency loop detected for package 'MimeKit'.
  At line:1 char:1
  + Install-Package -Name 'MimeKit' -Source &quot;https://www.nuget.org/api/v2 ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : Deadlock detected: (MimeKit:String) [Install-Package], Exception
  + FullyQualifiedErrorId : 
  DependencyLoopDetected,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
  PS C:\bin\ps1\renew_certs&gt; ($error[0].Exception.GetBaseException())
  Dependency loop detected for package 'MimeKit'.
  </code></pre>
  <p>Another answer here about dependency loop exceptions suggested to use the -SkipDependency parameter on Install-Package cmdlet, and this avoided the installation exception.</p>
  <p>I moved on to try to run my code however this resulted in an exception (very similar to the exception to this one this question is about) and in that case</p>
  <pre><code>PS C:\bin\ps1\renew_certs&gt; ($error[0].Exception.GetBaseException())
  </code></pre>
  <p>result string indicated the file or assembly that could not be loaded was the package <a href="https://www.nuget.org/packages/Portable.BouncyCastle/" rel="nofollow noreferrer">Portable.BouncyCastle</a> which is identified as a dependency of the MimeKit.</p>
  <p>So, I used Install-Package for Portable.BouncyCastle (I did not use -SkipDependencies so I assume they installed), and then in the code used Add-Type to load the DLL too.</p>
  <p>This seemed to prevent the BouncyCastle related exception; however, running the code now yields the exception that is the topic of my question:</p>
  <p>Code:</p>
  <pre><code>Add-Type -Path &quot;C:\Program Files\PackageManagement\NuGet\Packages\System.Text.Encoding.CodePages.6.0.0\lib\netstandard2.0\System.Text.Encoding.CodePages.dll&quot;
  Add-Type -Path &quot;C:\Program Files\PackageManagement\NuGet\Packages\Portable.BouncyCastle.1.9.0\lib\netstandard2.0\BouncyCastle.Crypto.dll&quot;
  Add-Type -Path &quot;C:\Program Files\PackageManagement\NuGet\Packages\MimeKit.3.3.0\lib\netstandard2.0\MimeKit.dll&quot;
  Add-Type -Path &quot;C:\Program Files\PackageManagement\NuGet\Packages\MailKit.3.3.0\lib\netstandard2.0\MailKit.dll&quot;
  
   
  $SMTP     = New-Object MailKit.Net.Smtp.SmtpClient
  $Message  = New-Object MimeKit.MimeMessage
  
  &lt;#
  $TextPart = [MimeKit.TextPart]::new(&quot;plain&quot;)
  $TextPart.Text = &quot;There was a deployment&quot;
  &lt;#
  </code></pre>
  <p>Exception:</p>
  <pre><code>PS C:\bin\ps1\renew_certs&gt; . &quot;c:\bin\ps1\renew_certs\send_email.ps1&quot;
  New-Object : Exception calling &quot;.ctor&quot; with &quot;0&quot; argument(s): &quot;The type initializer for 'MimeKit.ParserOptions' threw an exception.&quot;
  At C:\bin\ps1\renew_certs\send_email.ps1:9 char:13
  + $Message  = New-Object MimeKit.MimeMessage
  +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
      + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
   
  PS C:\bin\ps1\renew_certs&gt; ($error[0].Exception.GetBaseException())
  Could not load file or assembly 'System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its 
  dependencies. The system cannot find the file specified.
  PS C:\bin\ps1\renew_certs&gt;
  </code></pre>
  <p>The <a href="https://www.nuget.org/packages/System.Text.Encoding.CodePages/" rel="nofollow noreferrer">System.Text.Encoding.CodePages</a> is listed as a dependency of the MimeKit, so I tried to resolve similar to the BouncyCastle case, but System.Text.Encoding.CodePages install fails with a dependency loop too.  So, I did install with -SkipDependencies flag, and explicitly add DLL with Add-Type in my code; however, still the same exception.</p>
  <p>The dependency noted at <a href="https://www.nuget.org/packages/MimeKit/" rel="nofollow noreferrer">MimeKit</a> for <a href="https://www.nuget.org/packages/System.Text.Encoding.CodePages/" rel="nofollow noreferrer">System.Text.Encoding.CodePages</a> says (&gt;= 4.5.1) and the one installed was System.Text.Encoding.CodePages.6.0.0 directory.  The exception error string says file or assembly that could not be loaded was Version=4.1.1.0, but I don't see any 4.1.* versions at the gallery.</p>
  <p>Wondering if it was a dependency of System.Text.Encoding.CodePages, I then tried to install the System.Memory package but it also failed on a dependency loop error, and looking at <a href="https://www.nuget.org/packages/System.Memory/#dependencies-body-tab" rel="nofollow noreferrer">its dependencies tab</a> I was overwhelmed.</p>
  <p>Is there any way to see a trace of exactly why the dependency loop errors are occurring, or what files are actually missing?</p>
  <p>Is the issue perhaps something else entirely?</p>
  <p>The exception string says &quot;The system cannot find the file specified.&quot;  Is there anyway to see the exact filepath that is not found?</p>
  <p>When I ran the Powershell debugger in VS Code the exception is thrown stepping into [MimeKit.TextPart]::new(&quot;plain&quot;).  Anyway to debug down into the DLL?</p>