I've started working on a new project, a module with the goal of replacing Send-MailMessage
.
For those not familiar with Send-MailMessage
, or just aren't sure what you would do with it, it does what it says: it lets you send an email easily with PowerShell. While I'm sure there are a lot of fantastic use cases for this, the primary two things I use it for are to send alerts from my automated scripts when something goes wrong so I can look into it, and to email reports I'm automatically generating to the appropriate people. It can also be used as part of a system monitor to ensure that things are running smoothly.
Without Send-MailMessage
, we would need to manually create the client connector, build it up with the appropriate parts to make it work, create the email and build that up with what it needs, and send the email through the connector. Oh, and remember to dispose the connector when you're done with it. It's really a pain in the beehive.
Update 2022-03-14: Project name has changed from PSMailKit
to PoshMailKit
. Turns out someone created a project in the PowerShell Gallery with that name already, likely with a similar intent, but unlisted and abandoned it right afterward. Which is a shame, I liked PSMailKit
better, honestly.
The deprecation of Send-MailMessage
and System.Net.Mail.SmtpClient
If you've used Send-MailMessage
recently, you've likely seen this error message:
I guess this is going away...
If you go to the link for more information, it will let you know that Net.Mail.SmtpClient
(which Send-MailMessage
is based on) does not support many modern (and more secure) protocols. The recommendation is to use MailKit
instead.
Unfortunately, this doesn't help much if you're using PowerShell instead of C#, unless you're wanting to import this type library into your scripts and build emails manually, which isn't a great experience when you're used to Send-MailMessage
doing all that for you.
Send-MailKitMessage
Wait, did I want the mail kit, or the mail cat?
I did find a replacement for Send-MailMessage
that works with MailKit
instead of SmtpClient
, called Send-MailKitMessage
, but it has a few significant drawbacks in my opinion: it doesn't support Windows PowerShell 5 (though apparently the preview version 3.2.0 is supposed to add this support), it is missing a few options from Send-MailMessage
, and it takes input differently than Send-MailMessage
.
This is not to say that I think Send-MailKitMessage
is poorly made; I simply don't feel it fits what I believe the goals of a Send-MailMessage
replacement should be. It did, however, inspire me to start this project.
I want to be clear that while I've glanced at the code for Send-MailKitMessage
to get an idea what I might be getting myself into, I have no plans to use any of it myself. While I don't have anything against building on the work of others, I don't want it to feel to myself or others that I'm stealing the work of someone else.
PoshMailKit
and Send-MKMailMessage
Going to make some new tools
Due to what I see as shortfalls, I've created a new project, PoshMailKit
, to build another replacement for Send-MailMessage
based on MailKit
that I'm calling Send-MKMailMessage
(to avoid confusion with Send-MailKitMessage
). The goals for this cmdlet are first-and-foremost to replicate all existing functionality of Send-MailMessage
using MailKit
as its basis, and to do so by taking the same parameter input as Send-MailMessage
: if you're using Send-MailMessage
, no matter what way you're using it, my goal is for you to be able to just change the call to Send-MKMailMessage
and have it work the exact same way.
For obvious reasons (such as the reasons why SmtpClient
has been deprecated), there will likely be instances where Send-MKMailMessage
won't be able to behave in exactly the same way, if it would require the use of a kind of connection that SmtpClient
would have quietly allowed that it shouldn't have; for those cases, where they come up, I'll probably add a compatibility switch that will allow it to gracefully degrade the protocol at the cost of security rather than functionality. This would allow for it to still be compatible in these cases but with an explicit action by the user to show that they intend to use it in this way and do so understanding the risk.
Beyond Send-MailMessage
(and Send-MKMailMessage
)
Really going out there
Ultimately, I want to add additional useful functionality, such as support for inline attachments for HTML based mail bodies to embed images (something I already have to override the existing Send-MailMessage
to add for my uses), or connection types that are made available by MailKit
. Beyond Send-MKMailMessage
, though, I'm hoping to ultimately add additional cmdlets for managing mail messages, such as retrieving them from remote servers. I don't have all the details yet; my short-term focus is on replacing Send-MailMessage
.
If this works well, I plan to publish it to the PowerShell Gallery for all to use, and I'm releasing it with the MIT license which will mean anyone can adapt it for their purposes as needed.
If anyone wants to help, I'm more than happy for assistance and I've already opened several issues that will need to be address before the project can be declared feature-complete for the purposes of replacing Send-MailMessage
.
Current functionality and roadmap
Zoom Zoom!
As of this posting, I've gotten the very basics working: it can send unencrypted email to an SMTP server on a specified port, with a single recipient and sender, a subject line, and a plain text body. It takes all of these parameters by name only at this time. As a functional example, I'm calling it version 0.1-alpha.
v0.9-beta
I'm targeting the beta release to add support for the following:
- All recipient types
- Multiple recipients in each recipient type
- Attachments
- HTML-based body
- SSL
- Credentials/authentication
I believe this will address the majority of use cases that people have, leaving out only the more niche parts of Send-MailMessage
, at least from my estimation. If I'm correct, at this point it will be ready for most general use where Send-MailMessage
was used previously. I may at this point look into getting it published on the PowerShell Gallery, which will save people from having to download and build it themselves if they want to try it out (though I might also look into putting up a binary release as well).
v1.0
For the 1.0 release, I want it to function as a full drop-in replacement of Send-MailMessage
, and as such will need to add support for the following:
- Positional parameters
- Delivery notification options
- Encoding options
- Priority
- ReplyTo
Although this is not needed for drop-in replacement status, I'm also targeting to add Inline Attachment support for 1.0.
v-Next
Possibly will be a 2.0 release, but my target goal for the first major addition is to implement the additional features available to MailKit
that aren't part of Send-MailMessage
already for sending email. This is a bit off right now so I'm not going to give specifics to this yet; I haven't explored these functions yet myself.
Conclusion
Finally there
Hopefully this will be useful to people who relied on Send-MailMessage
and want to move to something using a supported and more secure platform.
Image attribution
See links to origin sites for proper attribution to creators
- Email envelope
- Mail Kat
- Felt toolbox
- modified to remove watermark
- re-shared with same license as original: CC BY-NC-SA 3.0
- Sunrise over earth
- Finish on concrete