0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Send Email with SMTP

Use the Send-MailMessage cmdlet with parameters like -From, -To, -Subject, -Body, and -SmtpServer to send an email in PowerShell, for example: Send-MailMessage -From 'you@example.com' -To 'friend@example.com' -Subject 'Hello' -Body 'Test email' -SmtpServer 'smtp.example.com'.
📋

Examples

InputSend-MailMessage -From 'me@domain.com' -To 'you@domain.com' -Subject 'Test' -Body 'Hello' -SmtpServer 'smtp.domain.com'
OutputEmail sent successfully to you@domain.com
InputSend-MailMessage -From 'admin@company.com' -To 'team@company.com' -Subject 'Meeting' -Body 'Meeting at 10 AM' -SmtpServer 'smtp.company.com' -Port 587 -UseSsl
OutputEmail sent successfully to team@company.com using SSL on port 587
InputSend-MailMessage -From 'user@example.com' -To 'user@example.com' -Subject 'Empty Body' -Body '' -SmtpServer 'smtp.example.com'
OutputEmail sent successfully with empty body
🧠

How to Think About It

To send an email with PowerShell, you need to specify who the email is from, who it is to, the subject, the message body, and the SMTP server that will send the email. You use the built-in Send-MailMessage cmdlet and provide these details as parameters.
📐

Algorithm

1
Get the sender email address
2
Get the recipient email address
3
Set the email subject and body content
4
Specify the SMTP server address (and port if needed)
5
Call the <code>Send-MailMessage</code> cmdlet with these parameters
6
Confirm the email was sent or handle errors
💻

Code

powershell
Send-MailMessage -From 'sender@example.com' -To 'receiver@example.com' -Subject 'Test Email' -Body 'This is a test email sent from PowerShell.' -SmtpServer 'smtp.example.com'
Write-Output 'Email sent successfully.'
Output
Email sent successfully.
🔍

Dry Run

Let's trace sending a test email through the script

1

Set parameters

From = 'sender@example.com', To = 'receiver@example.com', Subject = 'Test Email', Body = 'This is a test email sent from PowerShell.', SmtpServer = 'smtp.example.com'

2

Call Send-MailMessage

Send-MailMessage sends the email using the SMTP server

3

Print confirmation

Output: 'Email sent successfully.'

StepActionValue
1Set Fromsender@example.com
1Set Toreceiver@example.com
1Set SubjectTest Email
1Set BodyThis is a test email sent from PowerShell.
1Set SMTP Serversmtp.example.com
2Send-MailMessage calledEmail sent
3Output messageEmail sent successfully.
💡

Why This Works

Step 1: Specify sender and recipient

The -From and -To parameters tell PowerShell who is sending and receiving the email.

Step 2: Set subject and body

The -Subject and -Body parameters define the email's title and message content.

Step 3: Use SMTP server

The -SmtpServer parameter tells PowerShell which mail server to use to send the email.

Step 4: Send the email

The Send-MailMessage cmdlet sends the email using the provided details.

🔄

Alternative Approaches

Using .NET SmtpClient class
powershell
$smtp = New-Object System.Net.Mail.SmtpClient('smtp.example.com'); $mail = New-Object System.Net.Mail.MailMessage('sender@example.com','receiver@example.com','Subject','Body'); $smtp.Send($mail); Write-Output 'Email sent via .NET SmtpClient'
More control over email sending but requires more code and manual setup.
Using Send-MailMessage with credentials
powershell
$cred = Get-Credential; Send-MailMessage -From 'sender@example.com' -To 'receiver@example.com' -Subject 'Secure Email' -Body 'Body text' -SmtpServer 'smtp.example.com' -Credential $cred -UseSsl; Write-Output 'Email sent with authentication'
Use this to send emails through SMTP servers that require login and SSL.

Complexity: O(1) time, O(1) space

Time Complexity

Sending an email is a single operation with no loops, so it runs in constant time O(1).

Space Complexity

The script uses a fixed amount of memory for parameters and the email message, so space complexity is O(1).

Which Approach is Fastest?

Using Send-MailMessage is simpler and faster to write, while .NET SmtpClient offers more control but requires more code.

ApproachTimeSpaceBest For
Send-MailMessage cmdletO(1)O(1)Quick and simple email sending
.NET SmtpClient classO(1)O(1)Advanced control and customization
Send-MailMessage with credentialsO(1)O(1)Secure SMTP servers requiring login
💡
Always specify the SMTP server and use SSL if your server requires secure connections.
⚠️
Forgetting to specify the SMTP server or using an incorrect server address causes the email to fail sending.