简体   繁体   中英

PowerShell/EWS Send message with Delayed/Deferred delivery

I have a function that sends an email using delayed delivery, via COM objects. I am trying to write an equivalent function for EWS. I got a working PoC but it has 1 glaring issue: The message sits in the sent items folder, not the outbox, until the defer date arrives, then it sends as expected.

I'm using the SendAndSaveCopy method and I tried specifying the Outbox folder. This did put the message into the Outbox, but when it sends, it doesn't get moved to the Sent Items folder.

$service = New-EwsService # .... assume this works; My RequestedServerVersion is Exchange2013

$EmailMessage = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service 
$EmailMessage.Subject = 'outbox test'
$EmailMessage.Body = 'this is a test'
$EmailMessage.ToRecipients.Add('someone@somewhere.com')
$SendOn = (get-date).AddMinutes(5)
    $defer = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(
        0x3FEF, # 'PR_DEFERRED_SEND_TIME',
        [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime
    )
$EmailMessage.SetExtendedProperty($defer,$SendOn.ToUniversalTime())

# $EmailMessage.SendAndSaveCopy([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Outbox)  
$EmailMessage.SendAndSaveCopy()

Another thing I noticed was that if I open the message in Outlook while it's waiting to send, and I look at the delay delivery settings, it's not configured (in the Outlook UI). And if I hit send on that item, it immediately sends. I would assume that the config would be in the message and visible from the UI. (The expected behavior is the message in the Outbox list should be in italics until you open it, then it will return to italics if you hit send, otherwise it won't be italic and won't send when the defer time arrives).

Is there anyone who knows why the folder behavior doesn't match the MS doc ?

The message.SendAndSaveCopy() line results in a call to the service. If the call is successful, the email message will be available in the caller's Outbox folder. After the email message is sent, a copy of the message will be created in the Sent Items folder.

The docs you refer to are very old and the behaviour has changed. In Office 365, the deferred message is saved in Drafts folder until the time of sending, at which point it is sent and then saved to Sent Items.

Note that Outlook Object Model works against Outlook, while EWS works directly with the mailbox on Exchange.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM