This script demonstrates how to save email messages in the default Drafts folder of a shared mailbox in Office 365 Exchange Online.
Currently, Microsoft Office 365 Exchange Online allows you to create a shared mailbox. A shared mailbox allows a group of users to view and send e-mail from a common mailbox. But the email messages sent by these users on behalf of the shared mailbox actually appear in their Sent Items folder by default. You may want to use a script to save email messages in the default Draft folder of shared mailbox. Then you can send these email messages later and these email messages can be viewed by these uses.
This script contains the following function:
Send-OSCEXOEmailMessage
You can use this script in the following way.
Here are some code snippets for your reference.
PowerShellEdit|RemovepowershellTry { if (-not $SaveDraftInSharedMailbox) { $emailMessage.SendAndSaveCopy() } else { if ($exService.Credentials.Credentials.UserName -ne $From) { $mailbox = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($From) $folderID = New-Object Microsoft.Exchange.WebServices.Data.FolderId(` [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Drafts,` $mailbox) $emailMessage.Save($folderID) } } } Catch { $PSCmdlet.ThrowTerminatingError($_) }Try { if (-not $SaveDraftInSharedMailbox) { $emailMessage.SendAndSaveCopy() } else { if ($exService.Credentials.Credentials.UserName -ne $From) { $mailbox = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($From) $folderID = New-Object Microsoft.Exchange.WebServices.Data.FolderId(` [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Drafts,` $mailbox) $emailMessage.Save($folderID) } } } Catch { $PSCmdlet.ThrowTerminatingError($_) }
Example 1: How to display help about
Send-OSCEXOEmailMessage.
To display help about this function, please run this command.
Get-Help Send-OSCEXOEmailMessage -Full
Example 2: How to initiate a connection to Office 365 Exchange Online.
To initiate a connection to Office 365 Exchange Online, please run this command. You must run this example before any other steps.
Connect-OSCEXOWebService -Credential (Get-Credential admin@domain01.onmicrosoft.com)
Note The Connect-OSCEXOWebService function creates a new variable called
exService. This variable is in the global scope of the current Windows PowerShell session. This variable is used by other functions in the script.
Example 3: How to save email messages in the default Drafts folder of a shared mailbox.
To save email messages in the default Drafts folder of a shared mailbox, please run this command.
Send-OSCEXOEmailMessage -From "corphr@domain.onmicrosoft.com" -To "johnd@domain.onmicrosoft.com" -Subject "Save mail in the default Drafts folder of a Shared Mailbox" -Body "Save mail in the default Drafts folder of a Shared
Mailbox" -SaveDraftInSharedMailbox
Additional Resources
Technical Resources:
Windows PowerShell Advanced Function
Creating and sending email messages by using the EWS Managed API
How to: Delay sending an email message by using the EWS Managed API
Adding inline attachments by using the EWS Managed API
Set Up a Shared Mailbox
Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.