EWS Fundamentals Accessing and utilizing Shared mailboxes One of the crucial generally requested and misunderstood issues that individuals beginning out utilizing Change Net Companies get mistaken is accessing a Shared Mailbox or a Delegated Mailbox different then that of safety principal (one other approach of claiming credentials) you’re authenticating with.
Autodiscover
One of many first confusion factors is with Autodiscover, for individuals who aren’t that conversant in Change its vital to know that each one Autodisover does is provides you the endpoint to hook up with for Change Net Companies. Some folks confuse utilizing the next line
$service.AutodiscoverUrl(“Mailbox@domain.com”,{$true})
To imply all future EWS requests will go the mailbox you utilize right here which is not the case all it will do is return probably the most optimized endpoint for EWS request for that individual person.
Authentication
By default no person has entry to a Mailbox different then the proprietor of that mailbox, a standard drawback that individuals have is to imagine they’ll use the admin account to entry any customers Mailbox content material . Entry to a Mailbox must be granted by way of
- Including the person as a Delegate in Outlook or by way of the EWS Delegate operations
- Giving the person full entry utilizing Add-MailboxPermission within the Change Administration Shell
- Grant EWS Impersonations rights on the Mailbox by way of the Software Impersonation RBAC function
- Give Entry to explicit mailbox folder in Outlook or by way of Add-MailboxFolderPermssion
$folderid= new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
perform Get-FolderFromPath{
param (
[Parameter(Place=0, Necessary=$true)] [string]$FolderPath,
[Parameter(Place=1, Necessary=$true)] [string]$MailboxName,
[Parameter(Place=2, Necessary=$true)] [Microsoft.Exchange.WebServices.Data.ExchangeService]$service,
[Parameter(Place=3, Necessary=$false)] [Microsoft.Exchange.WebServices.Data.PropertySet]$PropertySet
)
course of{
## Discover and Bind to Folder primarily based on Path
#Outline the trail to look ought to be seperated with
#Bind to the MSGFolder Root
$folderid = new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#Cut up the Search path into an array
$fldArray = $FolderPath.Cut up("")
#Loop by way of the Cut up Array and do a Seek for every stage of folder
for ($lint = 1; $lint -lt $fldArray.Size; $lint++) {
#Carry out search primarily based on the displayname of every folder stage
$fvFolderView = new-object Microsoft.Change.WebServices.Information.FolderView(1)
if(![string]::IsNullOrEmpty($PropertySet)){
$fvFolderView.PropertySet = $PropertySet
}
$SfSearchFilter = new-object Microsoft.Change.WebServices.Information.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint])
$findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
if ($findFolderResults.TotalCount -gt 0){
foreach($folder in $findFolderResults.Folders){
$tfTargetFolder = $folder
}
}
else{
Write-host ("Error Folder Not Found check path and try again")
$tfTargetFolder = $null
break
}
}
if($tfTargetFolder -ne $null){
return [Microsoft.Exchange.WebServices.Data.Folder]$tfTargetFolder
}
else{
throw ("Folder Not found")
}
}
}
Sending E mail As a Shared Mailbox
The very first thing in you code you wish to do is bind to the SentItems folder of the Mailbox you wish to ship as eg
$folderid= new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$MailboxName)
$SentItems = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
Then if you create a message to Ship set the From deal with to the Mailbox you wish to SendAs and set the SentItems FolderId to the Goal mailbox so a replica of what you’ll be sending might be saved to the SentItems folder of that mailbox eg
perform Ship-EWSMessage {
param(
[Parameter(Place=0, Necessary=$true)] [string]$MailboxName,
[Parameter(Necessary=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Necessary=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Necessary=$false)] [string]$url,
[Parameter(Place=6, Necessary=$true)] [String]$To,
[Parameter(Place=7, Necessary=$true)] [String]$Topic,
[Parameter(Place=8, Necessary=$true)] [String]$Physique,
[Parameter(Place=9, Necessary=$false)] [String]$Attachment
)
Start
{
if($url){
$service = Join-Change -MailboxName $MailboxName -Credentials $Credentials -url $url
}
else{
$service = Join-Change -MailboxName $MailboxName -Credentials $Credentials
}
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Change.WebServices.Information.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$folderid= new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$MailboxName)
$SentItems = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$EmailMessage = New-Object Microsoft.Change.WebServices.Information.EmailMessage -ArgumentList $service
$EmailMessage.Topic = $Topic
#Add Recipients
$EmailMessage.ToRecipients.Add($To)
$EmailMessage.Physique = New-Object Microsoft.Change.WebServices.Information.MessageBody
$EmailMessage.Physique.BodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::HTML
$EmailMessage.Physique.Textual content = "Body"
$EmailMessage.From = $MailboxName
if($Attachment)
{
$EmailMessage.Attachments.AddFileAttachment($Attachment)
}
$EmailMessage.SendAndSaveCopy($SentItems.Id)
}
}