Skip to content

Instantly share code, notes, and snippets.

@sunnyc7
Created April 23, 2014 15:25
Show Gist options
  • Save sunnyc7/11219860 to your computer and use it in GitHub Desktop.
Save sunnyc7/11219860 to your computer and use it in GitHub Desktop.
Send-EmailMessage using Exchange Web Services
Function Send-EMailMessage {
[CmdletBinding()]
param(
[Parameter(Position=1, Mandatory=$true)]
[String[]]
$To,
[Parameter(Position=2, Mandatory=$false)]
[String[]]
$CcRecipients,
[Parameter(Position=3, Mandatory=$false)]
[String[]]
$BccRecipients,
[Parameter(Position=4, Mandatory=$true)]
[String]
$Subject,
[Parameter(Position=5, Mandatory=$true)]
[String]
$Body,
[Parameter(Position=6, Mandatory=$false)]
[Switch]
$BodyAsHtml,
[Parameter(Position=7, Mandatory=$true)]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Position=8, Mandatory=$false)]
[String]$Attachment
)
begin {
#Load the EWS Managed API Assembly
#Install EWS Assembly form here and install it.
# http://www.microsoft.com/en-us/download/details.aspx?id=28952
#Copy EWS Assembly
$EwsLocalPath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0"
if (!(Test-Path -Path $EwsLocalPath)) {
New-Item -ItemType Directory -Path $EwsLocalPath
Copy $EWSSharePath\*.* $EwsLocalPath
}
else {
Copy $EWSSharePath\*.* $EwsLocalPath
}
Add-Type -Path $EwsLocalPath\Microsoft.Exchange.WebServices.dll
}
process {
#Insatiate the EWS service object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2010_SP1
#Set the credentials for Exchange Online
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $Credential.UserName, $Credential.GetNetworkCredential().Password
#Find Email Address from UserName
$tmpUsername = ($Credential.UserName).Split("\")[1]
$query = "SELECT * FROM ds_user where ds_sAMAccountName='$tmpUsername'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
#Determine the EWS endpoint using autodiscover
$service.AutodiscoverUrl($user.DS_mail)
# Configure Exchange Impersonation.
# Impersonation Rights are granted by the following ROLE-ASSIGNMENT
# New-ManagementRoleAssignment –Name:EWSImpersonationTEST –Role:ApplicationImpersonation –User:"domain\svcImpersonationID"
# Remove Role Assignment
# Get-ManagementRoleAssignment -RoleAssigneeType User | Where {$_.Role -eq "ApplicationImpersonation" } | select -First 1 | Remove-ManagementRoleAssignment
# Add Impersonation
$MailboxName = "first.lastname@companydomain.com"
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
#Create the email message and set the Subject and Body
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
$message.Subject = $Subject
$message.Body = $Body
#If the -BodyAsHtml parameter is not used, send the message as plain text
if(!$BodyAsHtml) {
$message.Body.BodyType = 'Text'
}
if (Test-Path $Attachment) {
$message.Attachments.AddFileAttachment("$Attachment");
}
#Add each specified recipient
$To | ForEach-Object{
$null = $message.ToRecipients.Add($_)
}
#Add each specified carbon copy recipient
if($CcRecipients) {
$CcRecipients | ForEach-Object{
$null = $message.CcRecipients.Add($_)
}
}
#Add each specified blind copy recipient
if($BccRecipients) {
$BccRecipients | ForEach-Object{
$null = $message.BccRecipients.Add($_)
}
}
#Send the message and save a copy in the Sent Items folder
$message.SendAndSaveCopy()
} #End of Process Block
} #End of Function
#$credential = Get-Credential
$AttachmentPath = "C:\temp\myattachmentfile.csv"
$to = "sunnyc7@gmail.com"
$Subject = "Test email via Web Serices"
$Body = @"
This is the message Body.
You can include anything here alongwith notifications.
Adding more stuff here.
You can also use the body variable as parameter.
Time:: $(Get-date)
Sent from computer:: $env:computername
Sent by User:: $env:username
Thank You.
Sunny.
"@
$Splat = @{
To = $To
Subject = $Subject
Body =$Body
Credential = $cred2
Attachment = $AttachmentPath
}
Send-AAMEMailMessage @Splat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment