Using Mailkit with Powershell

David Marino 136 Reputation points
2021-03-09T15:43:55.097+00:00

I can run the attached PS script in the PS ISE, but it fails when I run it through a PS window. I can manually execute each command in the PS window without any issues. It looked like the subsequent commands did not "see" the previous commands, and I put in a sleep -s 5 commands after each of the previous commands leading up to the class definition. It did not matter, it failed very quickly. Using the -noexit -Sta parameters did not change the outcome. I ran PS as an Administrator, .\LoadMailkit.ps1 This is for internal use only to send alerts, and therefore I don't need any of the additional functions; attachments / use with gmail, etc. Thank you for your help. ---------- PS 5 Error: At F:\apps\Everbridge\PS\LoadMailKit.ps1:18 char:36 + class MailboxAddressExtended : MailboxAddress { #does not allow p ... PS 7 Error: ParserError: F:\Apps\Everbridge\PS\LoadMailKit.ps1:18 Line | 18 | class MailboxAddressExtended : MailboxAddress { #does not allow p … | ~~~~~~~~~~~~~~ | Unable to find type [MailboxAddress]. [75860-loadmailkit.txt][1] PS 5 Major Minor Build Revision ----- ----- ----- -------- 5 1 18362 1110 PS 7 Major Minor Patch PreReleaseLabel BuildLabel ----- ----- ----- --------------- ---------- 7 1 0 ---------- [1]: /api/attachments/75860-loadmailkit.txt?platform=QnA

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,456 questions
0 comments No comments
{count} votes

Accepted answer
  1. David Marino 136 Reputation points
    2021-03-10T15:41:31.67+00:00

    The attached file provides basic integration of MailKit / Mimekit with Powershell 5. Used only for sending basic text alerts from Powershell, which means it doesn't have any of the other features of MailKit / Mimekit; i.e. Attachments, authentication to external mail services, etc.

    76387-simplepsloadmailkitexample.txt

    Tested on PS 5
    Major Minor Build Revision
    ----- ----- ----- --------
    5 1 18362 1110

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,831 Reputation points
    2021-03-09T19:56:59.367+00:00

    Try dot-sourcing that LoadMailKit.ps1 script instead of just running it (note the two periods, separated by a space):

    . .\LoadMailkit.ps1  
    

    about_scripts