Easy way to replace PowerShell Send-MailMessage functionality?

Rick Angel 91 Reputation points
2022-04-19T19:29:19.56+00:00

Recently all my PowerShell scripts that use "Send-MailMessage" stopped working because because Microsoft stopped supporting basic authentication. I've looked at replacing the old code with an app password or with Microsoft Graph. The documentation I've seen is more complicated than I can grasp. I dabble with PowerShell, but I'm not a real developer. Can someone provide the code and steps required to translate the old syntax into something that will work? I don't really care which method I use. Here is an example from one old script that sends email when an error is found in a log file. Thanks.

Send-MailMessage -UseSsl -Body "Check \computer_name\share_name\log.txt for errors." -From $FromAddress -SmtpServer $SMTP_Server -Subject "Error found in log file" -To $ToAddress -Port $Port -Credential $smtpCred

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,430 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points
    2022-04-19T20:28:53.543+00:00

    Try using MailKit and MimeKit.

    mailkit
    Or, using NuGet: MailKit

    MimeKit
    Or, using NuGet: MimeKit

    I had a problem installing them, though. There's a problem with a circular dependency. Just install MimeKit first. If that goes okay, install MimeKit and use the "-SkipDependencies" switch. If you have a problem installing MimeKit, use -SkipDependencies on that install, too.


  2. Rich Matheisen 45,416 Reputation points
    2022-04-20T18:56:05.917+00:00

    There is no associated PowerShell module or cmdlet (from Microsoft) that uses MailKit. Using MailKit to send e-mail is somewhat like what you had to do before there was a Send-MailMessage cmdlet.

    Here's a link to an example: powershell-email

    The documentation for the module on GitHub might not be up-to-date with the most recent version, and the examples are in C# (but pretty easily transported to PowerShell): Examples

    There's also this module: 3.0.0