PowerShell Script to Send Email with SMTP
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
How to Think About It
Send-MailMessage cmdlet and provide these details as parameters.Algorithm
Code
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.'
Dry Run
Let's trace sending a test email through the script
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'
Call Send-MailMessage
Send-MailMessage sends the email using the SMTP server
Print confirmation
Output: 'Email sent successfully.'
| Step | Action | Value |
|---|---|---|
| 1 | Set From | sender@example.com |
| 1 | Set To | receiver@example.com |
| 1 | Set Subject | Test Email |
| 1 | Set Body | This is a test email sent from PowerShell. |
| 1 | Set SMTP Server | smtp.example.com |
| 2 | Send-MailMessage called | Email sent |
| 3 | Output message | Email 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
$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'
$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'
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Send-MailMessage cmdlet | O(1) | O(1) | Quick and simple email sending |
| .NET SmtpClient class | O(1) | O(1) | Advanced control and customization |
| Send-MailMessage with credentials | O(1) | O(1) | Secure SMTP servers requiring login |