How to Send SMTP Email Using PowerShell(转)用PowerShell实现SMTP邮件发送功能

转自:http://exchangeserverpro.com/powershell-how-to-send-email/

Simple Powershell function to send email

A useful technique for Exchange Server administrators is to be able to send email messages via SMTP from PowerShell. In this series of articles I will take you through different scripting techniques for sending email from your scripts.

There are a few different ways to do this, depending on the version of PowerShell installed on your computer or server. You can check the version number by typing $host in a PowerShell window.

PowerShell 1.0 will show this result:

Let’s take a look at how we can send SMTP email using each version of PowerShell.

参考注释:
<# This is a simple function that that sends a message.  The variables defined below can be passed as parameters by taking them out   and putting then in the parentheseis above.    i.e. "Function Mailer ($subject)"  #>

Sending SMTP Email with PowerShell 1.0

For PowerShell 1.0 we can send mail by running these commands.

PS C:\> $smtp = New-Object Net.Mail.SmtpClient("ho-ex2010-caht1.exchangeserverpro.net")
PS C:\> $smtp.Send("reports@exchangeserverpro.net","administrator@exchangeserverpro.net","Test Email","This is a test")

So what did we just do there? Let’s break it down.

  1. Created a new instance of a .NET object of class SmtpClient, connected to the SMTP server ho-ex2010-caht1 (a Hub Transport server)
  2. Used the Send method of the object to send an email message with the following:
    • From address of “reports@exchangeserverpro.net”
    • To address of “administrator@exchangeserverpro.net
    • Subject of “Test Email”
    • Body of “This is a test”

That works fine, though perhaps a bit cumbersome to type it out every time. Instead what we could do is create a script to send SMTP email using PowerShell 1.0.

#
#.SYNOPSIS
#Sends SMTP email via the Hub Transport server
#
#.EXAMPLE
#.\Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test"
#

param(
[string]$to,
[string]$subject,
[string]$body
)

$smtpServer = "ho-ex2010-caht1.exchangeserverpro.net"
$smtpFrom = "reports@exchangeserverpro.net"
$smtpTo = $to
$messageSubject = $subject
$messageBody = $body

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

We can save that code as Send-Email.ps1 and run it from the PowerShell window.

PS C:\Scripts> .\Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test"

Less typing required, especially when you hard-code some of the variables such as the From address and SMTP server.

Sending SMTP Email with PowerShell 2.0

PowerShell 2.0 makes life a little easier thanks to the built in cmdlet Send-MailMessage. To send the same email as the above example we would run this command:

PS C:\> Send-MailMessage -From "reports@exchangeserverpro.net" -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test email"

One of the first things you might notice in the command above is that no SMTP server was specified. Send-MailMessage does have a -SmtpServer parameter, but if you don’t specify one it will just use the $PSEmailServer preference variable instead. This can be set for the session by running the following command:

PS C:\> $PSEmailServer = "ho-ex2010-caht1.exchangeserverpro.net"

One of the first things you might notice in the command above is that no SMTP server was specified. Send-MailMessage does have a -SmtpServer parameter, but if you don’t specify one it will just use the $PSEmailServer preference variable instead. This can be set for the session by running the following command:

PS C:\> $PSEmailServer = "ho-ex2010-caht1.exchangeserverpro.net"

One of the first things you might notice in the command above is that no SMTP server was specified. Send-MailMessage does have a -SmtpServer parameter, but if you don’t specify one it will just use the $PSEmailServer preference variable instead. This can be set for the session by running the following command:

PS C:\> $PSEmailServer = "ho-ex2010-caht1.exchangeserverpro.net"

Another point to note with Send-MailMessage is that is uses authenticated SMTP connections. By default it will use the credentials of the user executing the command. So you need to make sure you’re using an SMTP server that either:

Those are just a few simple examples of how to send email using SMTP and PowerShell 1.0 or 2.0.

posted @ 2014-06-16 14:27  IT麦兜  阅读(655)  评论(0编辑  收藏  举报