I am working to build a script that utilizes MailKit and MimeKit to send an email using PowerShell. I built a runnable version of this script on my development machine and am now trying to get it working on the server. The first issue I encountered was that I was not being able to use Add-Type like I was on my development machine. When using Add-Type on the server I wish to deploy the script on, I get the following error
(This may be the root of my problem). What I did as a workaround was to use
[System.Reflection.Assembly]::LoadFrom($PathToDLL)
and it works as expected. To verify it worked, I loaded the MailKit assembly using the above command and was able to instantiate the SMTPClient Object by doing $SMTP = New-Object MailKit.Net.Smtp.SmtpClient
however if I load the DLL for MimeKit using [System.Reflection.Assembly]::LoadFrom
and then try to instantiate the object by running New-Object MimeKit.MimeMessage
I get an error Exception calling ".ctor" with "0" arguments": could not load file or assembly 'System.Memory'...
My next steps were to try installing System.Memory using NuGet, then use LoadFrom
to import the Assembly into my current session. Once System.Memory is imported, I again try to instantiate Mimekit.MimeMessage and receive the same error but instead of not being able to load System.Memory, it is not able to load System.Buffers. I follow the same procedure to download System.Buffers from Nuget and import it successfully with [System.Reflection.Assembly]::LoadFrom($PathToSystemBuffersDLL)
but I still receive Exception calling ".ctor" with "0" arguments": could not load file or assembly 'System.Buffers version=4.0.2.0., Culture=nuetral etc. The system could not find the file specified.
I have tried reinstalling the packages with NuGet, tried importing other versions including net462,net461, and any others to no avail. I've done pretty extensive research but I'm a bit stuck as to what to try next. I think something pretty relevant to what I'm experiencing is described in an answer here Cannot Load Assemblies For .Net Standard library (System.Text.Json). I used Install-Package -source 'nuget' $PackageName
to install my packages. It's worth noting that to install these packages I had to use Version 2 of NuGet's API. (https://www.nuget.org/api/v2)
Code that generates error on Windows Server 2016, but not on Windows 10 Pro. Both using PowerShell 5 and .NET Framework 4.8 (4.8.03761 on server and 4.8.04084 on Windows 10)
#Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\MailKit.3.4.1\lib\netstandard2.0\MailKit.dll" #Gives error
#Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\MimeKit.3.4.1\lib\netstandard2.0\MimeKit.dll" #gives error
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\PackageManagement\NuGet\Packages\MailKit.3.4.1\lib\netstandard2.0\MailKit.dll") #success
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\PackageManagement\NuGet\Packages\MimeKit.3.4.1\lib\netstandard2.0\MimeKit.dll") #success
$SMTP = New-Object MailKit.Net.Smtp.SmtpClient #works after loading mailkit assembly
$Message = New-Object MimeKit.MimeMessage #complains about System.Memory, if I run LoadFrom and add the System.Memory.dll package, will complain about System.Buffers.dll