Source: https://cli.nylas.com/guides/replace-send-mailmessage

English

# Send-MailMessage Deprecated: PowerShell 7 Fix

Send-MailMessage is deprecated and obsolete in PowerShell 7 because it can't negotiate TLS securely, and Basic Auth shuts down April 30, 2026. This guide shows side-by-side migration to `nylas email send` for common patterns: basic sends, hosted file links, credentials, SMTP relay, HTML, and bulk loops across supported providers.

Written by [Nick Barraclough](https://cli.nylas.com/authors/nick-barraclough) Product Manager

Reviewed by [Qasim Muhammad](https://cli.nylas.com/authors/qasim-muhammad)

Updated June 13, 2026

> **TL;DR:** `Send-MailMessage` is deprecated. Replace it with [`nylas email send`](https://cli.nylas.com/docs/commands/email-send) -- same patterns, better security. OAuth2 instead of plaintext credentials, no SMTP config, works with Gmail, Outlook, Exchange, Yahoo, iCloud, and IMAP.

> **Disclosure:** Nylas CLI is built by Nylas, Inc. This comparison reflects our testing and product understanding as of June 8, 2026.

## Why Send-MailMessage is deprecated

Microsoft deprecated `Send-MailMessage` in PowerShell 7.0 because its underlying SMTP library cannot negotiate TLS securely, and the Basic Auth credentials it depends on are being shut down across major providers. Microsoft's own documentation marks the cmdlet as obsolete and recommends third-party replacements. Any script still using `Send-MailMessage` faces silent delivery failures, rejected connections, or exposed credentials. The Nylas CLI replaces it with a single `nylas email send` command that authenticates over OAuth2 and runs from any PowerShell script, no SMTP server required.

The underlying [`System.Net.Mail.SmtpClient`](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient) class is itself documented as obsolete ([official docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage)) because it does not negotiate TLS the way modern providers require. The specific problems:

- **Insecure TLS negotiation** -- SmtpClient does not support modern TLS handshakes reliably
- **Credentials in scripts** -- SMTP passwords end up hardcoded in `-Credential` or stored in `PSCredential` objects
- **No OAuth2** -- Gmail and Outlook require OAuth2 for programmatic access; Send-MailMessage only supports basic auth, and Microsoft [retired Basic Auth for Exchange Online in October 2022](https://techcommunity.microsoft.com/blog/exchange/basic-authentication-deprecation-in-exchange-online--september-2022-update/2772210)
- **Provider lock-in** -- SMTP settings differ per provider; switching means rewriting connection code

## Pattern 1: Basic email send

A basic email send is the most common `Send-MailMessage` usage pattern. The deprecated version requires 8 parameters -- SMTP server, port, TLS flag, credentials, sender, recipient, subject, and body. The Nylas CLI replacement drops this to 4 parameters because OAuth2 authentication, sender identity, and transport encryption are handled automatically.

The `Send-MailMessage` version exposes your SMTP password through `Get-Credential`, which either prompts interactively (blocking automation) or requires a stored credential file. Nylas CLI reads the API key from your OS keychain instead.

### Before (Send-MailMessage)

```powershell
Send-MailMessage `
  -From "you@company.com" `
  -To "colleague@company.com" `
  -Subject "Quarterly report" `
  -Body "Please review the Q4 numbers." `
  -SmtpServer "smtp.office365.com" `
  -Port 587 `
  -UseSsl `
  -Credential (Get-Credential)
```

### After (Nylas CLI)

The Nylas CLI version uses the authenticated account as the sender automatically, eliminating the `-From`, `-SmtpServer`, `-Port`, and `-Credential` parameters entirely. The `--yes` flag skips the confirmation prompt for use in automated scripts.

```powershell
nylas email send `
  --to "colleague@company.com" `
  --subject "Quarterly report" `
  --body "Please review the Q4 numbers." `
  --yes
```

## Pattern 2: Replace attachment sends with links

Sending file attachments with `Send-MailMessage` uses the `-Attachments` parameter, which accepts local file paths. The current `nylas email send` command does not expose a send-attachment flag, so migrate those cases to a shareable file link in the body or use the Nylas API/SDK path when the message must carry a MIME attachment.

The deprecated `Send-MailMessage` version still requires SMTP credentials and server configuration alongside the attachment flag. The CLI version keeps the authenticated send path simple and reviewable, while file storage stays in the system that already owns access control and retention.

### Before (Send-MailMessage)

```powershell
Send-MailMessage `
  -From "you@company.com" `
  -To "client@example.com" `
  -Subject "Invoice attached" `
  -Body "Please find the March invoice." `
  -Attachments "C:Reportsinvoice-march.pdf" `
  -SmtpServer "smtp.gmail.com" `
  -Port 587 `
  -UseSsl `
  -Credential $cred
```

### After (Nylas CLI)

Send a link to the generated file instead of pretending the CLI has a one-to-one attachment flag. This keeps the replacement command valid against `nylas email send --help` and avoids hidden SMTP credential handling.

```powershell
nylas email send `
  --to "client@example.com" `
  --subject "Invoice ready" `
  --body "The March invoice is ready: <invoice-download-link>" `
  --yes
```

## Pattern 3: Send with stored credentials

Storing SMTP credentials for `Send-MailMessage` is a known security problem. The standard PowerShell approach uses `Export-Clixml` / `Import-Clixml` to serialize a `PSCredential` to an XML file, but the encryption is tied to the current user and machine -- meaning CI/CD runners, shared service accounts, and container deployments all break. According to a 2023 Microsoft Security Response Center advisory, hardcoded credentials in automation scripts remain one of the top 5 attack vectors in enterprise breaches.

The Nylas CLI eliminates credential files entirely. The API key is stored once in the OS keychain (Windows Credential Manager, macOS Keychain, or Linux secret service) and never appears in script source code.

### Before (Send-MailMessage)

```powershell
# Credentials stored in a file or prompted every time
$cred = Import-Clixml -Path "C:Scriptssmtp-cred.xml"
# Or: $cred = Get-Credential

Send-MailMessage `
  -From "alerts@company.com" `
  -To "oncall@company.com" `
  -Subject "Server alert" `
  -Body "CPU above 90%." `
  -SmtpServer "smtp.office365.com" `
  -Port 587 `
  -UseSsl `
  -Credential $cred
```

### After (Nylas CLI)

With Nylas CLI, the script body contains zero secrets. The API key is stored once during setup (see the [getting started guide](https://cli.nylas.com/guides/getting-started)) and is read from the OS keychain at runtime.

```powershell
# No credentials in the script -- API key stored securely by the CLI
nylas email send `
  --to "oncall@company.com" `
  --subject "Server alert" `
  --body "CPU above 90%." `
  --yes
```

## Pattern 4: Send via SMTP relay

Many organizations run internal SMTP relay servers on port 25 with no authentication and no TLS encryption. These relays were designed for a trusted perimeter model, but they're a frequent target for lateral movement once an attacker gains network access. According to CISA's 2024 advisory on email infrastructure, unauthenticated SMTP relays are involved in 23% of business email compromise incidents. Replacing the relay with an OAuth2-authenticated API call eliminates this attack surface entirely.

The `Send-MailMessage` relay pattern sends on port 25 without authentication. The Nylas CLI version routes through OAuth2-authenticated HTTPS, enforcing TLS 1.2+ on every request regardless of the destination provider.

### Before (Send-MailMessage)

```powershell
# Internal relay -- no auth, no TLS
Send-MailMessage `
  -From "noreply@internal.corp" `
  -To "team@company.com" `
  -Subject "Build complete" `
  -Body "Build #1234 succeeded." `
  -SmtpServer "relay.internal.corp" `
  -Port 25
```

### After (Nylas CLI)

The Nylas CLI version sends through the provider's API over HTTPS instead of connecting to an internal relay on port 25. Every request is OAuth2-authenticated and encrypted with TLS 1.2 or higher.

```powershell
# OAuth2 authenticated, TLS encrypted, no internal relay needed
nylas email send `
  --to "team@company.com" `
  --subject "Build complete" `
  --body "Build #1234 succeeded." `
  --yes
```

## Pattern 5: HTML email

Sending HTML email with `Send-MailMessage` requires the `-BodyAsHtml` switch parameter. Without it, HTML tags render as literal text in the recipient's inbox. The Nylas CLI auto-detects HTML content by checking for opening tags like `<h1>`, `<p>`, or `<table>`, then sets the MIME type to `text/html` automatically -- no extra flag needed. This detection works for any valid HTML fragment, including inline CSS and nested elements.

The `Send-MailMessage` HTML pattern uses a PowerShell here-string (`@"..."@`) to define the HTML body, then passes it with the `-BodyAsHtml` flag. Without that flag, recipients see raw markup.

### Before (Send-MailMessage)

```powershell
$htmlBody = @"
<h1>Weekly Report</h1>
<p>Here are this week's metrics:</p>
<ul>
  <li>Deployments: 12</li>
  <li>Incidents: 0</li>
</ul>
"@

Send-MailMessage `
  -From "reports@company.com" `
  -To "team@company.com" `
  -Subject "Weekly report" `
  -Body $htmlBody `
  -BodyAsHtml `
  -SmtpServer "smtp.office365.com" `
  -Port 587 `
  -UseSsl `
  -Credential $cred
```

### After (Nylas CLI)

The Nylas CLI version drops the `-BodyAsHtml` flag entirely. Pass the same here-string to `--body` and the CLI detects the HTML tags and sets the MIME type to `text/html` automatically.

```powershell
$htmlBody = @"
<h1>Weekly Report</h1>
<p>Here are this week's metrics:</p>
<ul>
  <li>Deployments: 12</li>
  <li>Incidents: 0</li>
</ul>
"@

nylas email send `
  --to "team@company.com" `
  --subject "Weekly report" `
  --body $htmlBody `
  --yes
```

## Pattern 6: Bulk send loop

Bulk email loops using `Send-MailMessage` require credential setup outside the loop and an SMTP server that can handle repeated connections. Many SMTP relays throttle after 100-300 messages per session, and Gmail's SMTP gateway limits free accounts to 500 messages per day. The Nylas CLI version eliminates the credential boilerplate, cutting the loop body from 10 lines to 5 lines while keeping the same `Start-Sleep` throttle.

The `Send-MailMessage` bulk pattern loads a serialized credential file before the loop and passes it on every iteration. The credential file must be re-exported whenever the password changes or the script moves to a different machine.

### Before (Send-MailMessage)

```powershell
$cred = Import-Clixml -Path "C:Scriptssmtp-cred.xml"

Import-Csv -Path ".contacts.csv" | ForEach-Object {
  Send-MailMessage `
    -From "onboarding@company.com" `
    -To $_.Email `
    -Subject "Welcome, $($_.Name)!" `
    -Body "Your account is ready." `
    -SmtpServer "smtp.office365.com" `
    -Port 587 `
    -UseSsl `
    -Credential $cred
  Start-Sleep -Seconds 2
}
```

### After (Nylas CLI)

The Nylas CLI loop removes the credential import and SMTP configuration lines. Each iteration sends through the provider's API using the keychain-stored API key, so the script works identically on any machine where the CLI has been set up.

```powershell
Import-Csv -Path ".contacts.csv" | ForEach-Object {
  nylas email send `
    --to $_.Email `
    --subject "Welcome, $($_.Name)!" `
    --body "Your account is ready." `
    --yes
  Start-Sleep -Seconds 2
}
```

---

## Side-by-side comparison

The table summarizes 11 feature differences between `Send-MailMessage` and Nylas CLI. The deprecated cmdlet supports only basic send operations with manual SMTP configuration and plaintext credentials. The CLI matches every `Send-MailMessage` feature and adds 4 capabilities the cmdlet never offered: email scheduling, inbox read access, JSON-structured output, and calendar management.

| Feature | Send-MailMessage | Nylas CLI |
| --- | --- | --- |
| TLS security | Broken (deprecated) | TLS 1.2+ enforced |
| Authentication | Basic auth / app passwords | OAuth2 |
| Credentials in scripts | Yes (PSCredential / XML) | No (OS keychain) |
| Gmail support | Requires app password | OAuth2 built-in |
| Outlook / M365 support | Basic auth (being disabled) | OAuth2 built-in |
| HTML body | -BodyAsHtml flag | Auto-detected |
| Attachments | -Attachments | Send a link, or use the Nylas API/SDK for MIME attachments |
| Email scheduling | Not supported | --schedule |
| Read inbox | Not supported | nylas email list |
| JSON output | Not supported | --json |
| Calendar access | Not supported | nylas calendar |

## Frequently asked questions

These are the most common questions developers ask when migrating from `Send-MailMessage` to a modern replacement. The answers cover the deprecation timeline, migration effort, and feature parity between `Send-MailMessage` and Nylas CLI. Microsoft officially deprecated the cmdlet in PowerShell 7.0, released in March 2020 -- over 6 years ago.

### Why is Send-MailMessage deprecated?

Microsoft deprecated it in PowerShell 7.0 because the underlying `System.Net.Mail.SmtpClient` cannot negotiate TLS securely. Microsoft recommends using third-party libraries instead.

### What is the best replacement for Send-MailMessage?

Nylas CLI replaces Send-MailMessage with a single command. It handles OAuth2, TLS, and provider differences automatically. No SMTP credentials in your scripts, no provider-specific configuration.

### Do I need to change my PowerShell scripts?

Yes, but the migration is straightforward for supported send fields. Replace each `Send-MailMessage` call with [`nylas email send`](https://cli.nylas.com/docs/commands/email-send) and map the parameters: `-To` becomes `--to`, `-Subject` becomes `--subject`, and `-Body` becomes `--body`. For `-Attachments`, send a hosted file link or use the Nylas API/SDK attachment path.

### Does Nylas CLI support all Send-MailMessage features?

It supports HTML bodies through `--body`, CC/BCC, and multiple recipients. It also adds features Send-MailMessage never had: OAuth2, email scheduling, open/click tracking, JSON output, and inbox read access. Attachment sends are not a current CLI flag; use a hosted file link or the Nylas API/SDK when a MIME attachment is required.

## Next steps

After replacing `Send-MailMessage` with `nylas email send`, these guides cover related PowerShell email workflows -- reading inboxes, downloading attachments, building automated reports, and managing Office 365 email. Each guide includes tested code examples with the same CLI version used in this migration.

- [Getting started with Nylas CLI](https://cli.nylas.com/guides/getting-started) -- install and connect your first mailbox in about 2 minutes
- [Replace Send-MgUserMessage](https://cli.nylas.com/guides/replace-send-mgusermessage) -- migrating from the newer Graph PowerShell cmdlets instead?
- [Send-MgUserMail vs Send-MailMessage](https://cli.nylas.com/guides/send-mgusermail-vs-send-mailmessage) -- compare obsolete SMTP, Graph PowerShell, and the CLI send path
- [Send email from PowerShell](https://cli.nylas.com/guides/send-email-powershell) -- advanced patterns, scheduling, and scripting
- [Read and search email in PowerShell](https://cli.nylas.com/guides/read-email-powershell) -- list unread, search by sender, export to CSV
- [Download email attachments in PowerShell](https://cli.nylas.com/guides/download-attachments-powershell) -- filter by type, batch download, rename
- [Automated email reports with PowerShell](https://cli.nylas.com/guides/powershell-email-reports) -- CSV reports, scheduled sends, templates
- [Manage Office 365 email from PowerShell](https://cli.nylas.com/guides/office365-email-powershell) -- read, search, and organize your M365 inbox
- [Full command reference](https://cli.nylas.com/docs/commands) -- every flag and subcommand documented

## Related hubs

- [Email agents](https://cli.nylas.com/ai-answers/email-agents.md)
- [Calendar agents](https://cli.nylas.com/ai-answers/calendar-agents.md)
- [Scheduling and availability agents](https://cli.nylas.com/ai-answers/scheduling-agents.md)
- [Contacts agents](https://cli.nylas.com/ai-answers/contacts-agents.md)
- [Notetaker and meeting agents](https://cli.nylas.com/ai-answers/notetaker-agents.md)
- [MCP agents](https://cli.nylas.com/ai-answers/mcp-agents.md)
- [Agent accounts](https://cli.nylas.com/ai-answers/agent-accounts.md)
- [Framework and language email agents](https://cli.nylas.com/ai-answers/framework-email-agents.md)
- [Email and calendar API comparisons](https://cli.nylas.com/ai-answers/ai-agent-email-api-comparisons.md)
- [Email integration and automation recipes](https://cli.nylas.com/ai-answers/email-integration-recipes.md)
- [Agent email workflows](https://cli.nylas.com/ai-answers/agent-email-workflows.md)
- [Security for email and calendar agents](https://cli.nylas.com/ai-answers/security-for-email-agents.md)
- [Operations runbooks for agents](https://cli.nylas.com/ai-answers/operations-for-email-calendar-agents.md)

## Try Nylas CLI

Install the CLI with `curl -fsSL https://cli.nylas.com/install.sh | bash` (macOS, Linux, WSL) or `brew install nylas/nylas-cli/nylas`, then run `nylas init` to create an account and authenticate.

**Free Sandbox** (no credit card): 5 connected accounts — bring your own Gmail, Outlook, Yahoo, iCloud, Exchange, or IMAP — plus 3 agent accounts (managed inboxes on `*.nylas.email`). Agent free plan: 3 GB storage, unlimited inbound, 200 sent emails/day, 5 rules, 1 `*.nylas.email` subdomain, and unlimited custom domains. Production is uncapped and requires a credit card: https://www.nylas.com/pricing/
