Send Emails until a Designated Time in Office 365 Exchange Online
Introduction
This script can send an email message with Office 365 Exchange Online. Also this script allows you to delay sending an email message.
Scenarios
Microsoft Outlook has a feature that enables you to delay sending email messages. However, this feature is not available in the Outlook Web App (OWA). Therefore, in order to delay sending email messages for some business needs, you must find a workaround.
Script
This script contains the following function:
You can use this script in the following way.
- Open Windows PowerShell.
- Run Import-Module cmdlet to import this
module file.
Import-Module filepath\scriptname.psm1
Here are some code snippets for your references.
PowerShell
Edit|Remove
powershell
#Add extended properties
if ($DeferredSendTime -ne $null) {
#Identify the extended properties that are used to set the time when the email message is sent.
$pidTagDeferredSendTime = 16367
$prDeferredSendTime = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(`
$pidTagDeferredSendTime,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime)
#Identify when the email message will be sent and delivered.
$emailMessage.SetExtendedProperty($prDeferredSendTime,$sendTime)
}
#Add extended propertiesif ($DeferredSendTime-ne $null) {
#Identify the extended properties that are used to set the time when the email message is sent.$pidTagDeferredSendTime = 16367
$prDeferredSendTime = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(`
$pidTagDeferredSendTime,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime)
#Identify when the email message will be sent and delivered.$emailMessage.SetExtendedProperty($prDeferredSendTime,$sendTime)
}
Examples
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 step.
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 delay sending an email message to user01@domain.onmicrosoft.com.
To delay sending an email message to user01@domain.onmicrosoft.com, please run this command.
Send-OSCEXOEmailMessage -To "user01@domain.onmicrosoft.com" -Subject "Test Mail" -DeferredSendTime "12/7/2012 3:00 PM"

Note The deferred send time is the date and time of your local computer. It will be converted to Coordinated Universal Time (UTC) by default. You do not need to convert it manually.
Example 4: How to send an e-mail message from user01@domain.onmicrosoft.com to
user02@domain.onmicrosoft.com.
To send an e-mail message from
user01@domain.onmicrosoft.com to user02@domain.onmicrosoft.com, please run this command.
Send-OSCEXOEmailMessage -From "user01@domain.onmicrosoft.com" -To "user02@domain.onmicrosoft.com" -Subject "Test Mail"

Note The
Send-OSCEXOEmailMessage
uses the account that initiates the connection to Office 365 Exchange Online for sending email messages by default. This example allows you to send an email message on behalf of another account.
Example 5: How to send an email message to user01@domain.onmicrosoft.com and user02@domain.onmicrosoft.com with a delivery receipt request.
To send an email message to
user01@domain.onmicrosoft.com and user02@domain.onmicrosoft.com with a delivery receipt request, please run this command.
Send-OSCEXOEmailMessage -To "user01@domain.onmicrosoft.com","user02@domain.onmicrosoft.com" -Subject "Processes" -Body (Get-Process | Select ProcessName,CPU | ConvertTo-Html | Out-String) -RequestDeliveryReceipt

Example 6: How to send an email message to user01@domain.onmicrosoft.com with inline attachments.
To send an email message to user01@domain.onmicrosoft.com with inline attachments, you need to create the HTML body with the content identifier of the attachments and use InlineAttachments parameter. After that, please run this command.
Send-OSCEXOEmailMessage -To "user01@domain.onmicrosoft.com" -Subject "Happy Birthday!" -Body (Get-Content "C:\Scripts\050\happybirthday.htm" | Out-String) -Attachments "C:\Scripts\050\giftcard.txt" -InlineAttachments "C:\Scripts\050\image001.png"

Additional Resources