<#
This file is used for Send mail via exchange service
owner: farawayplace613, tomcat.cheng@gmail.com
date: 2012/08/23
#>
# Load the Microsoft.Exchange.WebServices.dll which used for commicating with exchange server
$currentPath = (get-location).Path
$dllPath = '{0}\Microsoft.Exchange.WebServices.dll' -f $currentPath
[Reflection.Assembly]::LoadFile($dllPath)
# 3 standard for ExchangeVersion.Exchange2010_SP2
$exchangeService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService(3)
# Create the credentails which used for connecting to the exchange server
$exchangeService.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("YourUserName", "YourPassword", "YourDomainName")
# configure the url of the exchange services
$exchangeService.Url = new-object System.Uri("https://YourExchangeServerName/EWS/Exchange.asmx")
# Crate a mail
$message = new-object Microsoft.Exchange.WebServices.Data.EmailMessage($exchangeService)
$exchangeService.AutodiscoverUrl("xxxx@xxxx.com")
$message.Subject = "Hello word from power shell via exchange service"
$message.Body = "Sent using the EWS Managed API"
$message.ToRecipients.Add("xxxx1@xxxxx.com")
# send out the mail
$message.Send()
1. You can get the Microsoft.Exchange.WebServices.dll from http://download.microsoft.com/download/1/9/0/190EDCB6-00D2-41E1-ACFA-399E045B785A/EwsManagedApi.msi, copy the Microsoft.Exchange.WebServices.dll to the same floder of your power shell file.
2. You need to install the .Net framework 4.5 from http://www.microsoft.com/zh-cn/download/details.aspx?id=30653
3. You need to allow powershell use the latest .net framwork
PowerShell (the engine) runs fine under .NET 4.0. PowerShell ( the console host and the ISE) do not, simply because they were compiled against older versions of .NET. There's a registry setting that willchange the .NET framework loaded systemwide, which will in turn allow PowerShell to use .NET 4.0 classes:
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
To update just the ISE to use .NET 4.0, you can change the config ($psHome\powershell_ise.exe.config) file to have a chunk like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0.30319" />
</startup>
</configuration>
You can build .NET 4.0 applications that call PowerShell using the PowerShell API (System.Management.Automation.PowerShell) just fine, but these steps will help get the in-the-box PowerShell hosts to work under .NET 4.0
Note:
Don't use this solution on the same server which installed SharePoint 2010, becuase it will cause sharePoint stop work since Moss 2010 doesn't support the .Net framework 4.0