Skip to content

Update MailKit step template to version 2 - #1720

Open
harrisonmeister wants to merge 1 commit into
masterfrom
mh/send-mail-mailkit-fix
Open

harrisonmeister wants to merge 1 commit into
masterfrom
mh/send-mail-mailkit-fix

Conversation

@harrisonmeister

Copy link
Copy Markdown
Contributor

Background

MimeKit and MailKit 5.0.0 broke the Send Email using MailKit step in three ways:

  1. MimeKit is now a meta-package with no assemblies. The library code moved to MimeKit.Core (and MimeKit.Cryptography), so the hardcoded path lib/netstandard2.1/MimeKit.dll no longer exists and Add-Type fails.
  2. The step hardcoded the netstandard2.1 target folder, which isn't guaranteed to exist in every package version.
  3. SmtpClient.Send(MimeMessage) was removed. MailKit 5 expects Send(ISendRequest, CancellationToken) and returns a SendResult.

The step also picked up whatever version of each package was installed first, so a worker could end up loading MimeKit 4.x alongside MailKit 5.x.

Results

  • Installs and loads MimeKit.Core and MailKit instead of the MimeKit meta-package.
  • Pins both packages to the same version (default 5.0.0). A new optional parameter, SendEmail.MailKitVersion, overrides it.
  • Picks the best lib folder for the PowerShell runtime (netX.0 up to the runtime version, then netstandard2.1, then netstandard2.0) instead of hardcoding one. If nothing fits, the error lists the folders that do exist.
  • Get-Package now filters by -ProviderName NuGet and -RequiredVersion, so stale or unrelated packages on the worker aren't used.
  • Sends with [MailKit.SendRequest]::new($Message), logs the server response at verbose level and warns about rejected recipients.
  • Trims whitespace from comma-separated TO and CC addresses.
  • Disposes the SMTP client in a finally block so it's cleaned up when sending fails.
  • Version bumped from 1 to 2. Description updated to mention MimeKit.Core.

Before

Loading MimeKit:

Cannot find path '/home/tentacle/.local/share/PackageManagement/NuGet/Packages/MimeKit.5.0.0/lib/netstandard2.1/MimeKit.dll' because it does not exist.
At Script.ps1:56 char:16
+ Add-Type -Path $MimeKitPath

Sending (after working around the load failure):

Cannot find an overload for "Send" and the argument count: "1".
At Script.ps1:162 char:5
+     $SMTP.Send($Message)

After

Sending email... 
Email sent. 

Pre-requisites

  • Id should be a GUID that is not 00000000-0000-0000-0000-000000000000
    • NOTE If you are modifying an existing step template, please make sure that you do not modify the Id property (updating the Id will break the Library sync functionality in Octopus).
  • Version should be incremented, otherwise the integration with Octopus won't update the step template correctly
  • $Meta must have ExportedAt and OctopusVersion updated
  • Parameter names should not start with $
  • Step template parameter names (the ones declared in the JSON, not the script body) should be prefixed with a namespace so that they are less likely to clash with other user-defined variables in Octopus (see this issue). For example, use an abbreviated name of the step template or the category of the step template).
  • LastModifiedBy field must be present, and (optionally) updated with the correct author
  • The best practices documented here have been applied

Updated the MailKit step template to version 2, modifying the required packages to include MimeKit.Core instead of MimeKit. Adjusted the script body for compatibility with the new package structure and improved parameter handling.
@github-actions

Copy link
Copy Markdown

Review this PR in Hyponome for a side-by-side diff of the step-template JSON and any embedded scripts.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant