Powershell send email
PowerShell provides several ways to send emails using various modules and commands. Here are a few examples:
Using the Send-MailMessage cmdlet
The Send-MailMessage
cmdlet is a built-in PowerShell cmdlet that allows you to send emails using a variety of protocols, including SMTP, POP3, and IMAP.
Here's an example of how to use it:
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email"
You can also specify additional parameters, such as:
-SmtpServer
to specify the SMTP server to use-Port
to specify the port number to use-Credential
to specify the credentials to use for authentication-Attachments
to specify files to attach to the email
For example:
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email" -SmtpServer "smtp.example.com" -Port 587 -Credential (Get-Credential)
Using the Exchange PowerShell module
If you have Exchange Server installed, you can use the Exchange PowerShell module to send emails.
Here's an example of how to use it:
Import-Module Exchange
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email"
You can also use the New-MailMessage
cmdlet to create a new email message and then send it using the Send-MailMessage
cmdlet.
For example:
$mail = New-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email"
Send-MailMessage -To $mail.To -From $mail.From -Subject $mail.Subject -Body $mail.Body
Using a third-party module
There are several third-party modules available that provide additional functionality for sending emails in PowerShell. One popular example is the PowerShell-Mail
module.
Here's an example of how to use it:
Import-Module PowerShell-Mail
Send-Mail -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email"
You can also use the New-Mail
cmdlet to create a new email message and then send it using the Send-Mail
cmdlet.
For example:
$mail = New-Mail -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email"
Send-Mail -To $mail.To -From $mail.From -Subject $mail.Subject -Body $mail.Body