Introduction
I have written a small mail sending function in .NET that I leverage in all my systems I develop. I had to develop a bunch of Powershell scripts to monitor and report on various process and instead of going through the effort of reworking my .NET DLL and a Powershell function I opted to call the .NET DLL directly from Powershell.
My email DLL gives the ability to mail styled email messages. You can get the DLL from https://github.com/svermaak/downloads/raw/main/ITtelligence.Email_.dll_.zip
This method also produces the same result in Microsoft Outlook and other mail clients. See GMail preview of mail below.

Below you can see how to use this DLL to do the same from Powershell
Powershell
The first step is to make the DLL methods available in Powershell
[Reflection.Assembly]::LoadFile("C:\SOMEPATH\ITtelligence.Email\bin\Debug\ITtelligence.Email.dll")
One of the parameters that my DLL accepts is a list collection of string. To be able to pass this to the .NET DLL, first we create a string array
[string[]]$toEmailAddressesArray = "shaun.vermaak@ittelligence.com","someoneelse@ittelligence.com";
Once we have the string array we can cast it as a list collection type string
[Collections.Generic.List[String]]$toEmailAddressesList = $toEmailAddressesArray;
To make it easier to manage errors, the Send method returns a main result of type boolean based on the overall result and detailed error as a reference variable.
$ReturnMessage = "";
This is the .NET Send method definition
public static bool Send(List<string> MailAddresses, string Host, int Port, bool SSL, int Timeout, string Domain, string UserName, string Password, string From, string Subject, string Title, string Body, string LogoImage, string BackgroundImage, out string Ret
Based on the definition above, here’s an example of calling the Send method. The LogoImage and BackgroundImage can be physical locations to images or a base64 string using a service like https://www.base64-image.de/
[ITtelligence.EMail]::Send($mailAddresses, "in-v3.mailjet.com", 587, $true, 60000, "", "REMOVED", "REMOVED", "shaun.vermaak@ittelligence.com", "Sample Subject", "Sample Title", "Lorem ipsum dolor sit amet...", "c:\temp\ACME.png", "c:\Temp\Tile002.jpg",[ref] $ReturnMessage)
.NET DLL Source Code
If you are interested in the DLL source code, you can find it below or via this repo
Conclusion
I hope you found this tutorial useful. You are encouraged to ask questions, report any bugs or make any other comments about it below.
