none
How to create a template message in EWS to use with "serverreplywithmessage" rule? RRS feed

  • Question

  • Hi!

    Im trying to create a serverside rule for a mailbox using EWS.

    I want the mailbox to reply automatically to incomming emails.

    To do this i have to create a template message that can be used when specifying the rule action "ServerReplyWithMessage".

    I cant figure out how to create these templates..

    Im using Powershell and the EWS API. I have tried to create templates by doing this:

    $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
    $mail.Subject="Test"
    $mail.IsAssociated=$true
    $mail.ItemClass="IPM.Note.Rules.ReplyTemplate.Microsoft"
    $mail.Culture="en-US"
    $mail.body="Testing"
    $mail.save("Inbox")

    I then get the ID of the new message $mail.id and try to create a rule using this ID:

    $nrNewRule = New-Object Microsoft.Exchange.WebServices.Data.Rule
    $nrNewRule.DisplayName = "Test"
    $nrNewRule.Conditions.ContainsSubjectStrings.Add("test");

    $nrNewRule.Actions.ServerReplyWithMessage="messageid"

    $RuleOperation = New-Object Microsoft.Exchange.WebServices.Data.createRuleOperation[] 1
    $RuleOperation[0] = $nrNewRule

    $service.UpdateInboxRules($RuleOperation,$irInboxRules.OutlookRuleBlobExists)   

    The problem is that i keep getting this error "Exception calling "UpdateInboxRules" with "2" argument(s): "Exception of type 'Microsoft.Exchange.WebServices.Data.UpdateInboxRulesException' was thrown."

    And im guessing its because im not creating the template in a correct way...

    Wednesday, October 31, 2012 10:34 AM

Answers

  • You need to set the PidTagReplyTemplateId property on the Template messages as per http://msdn.microsoft.com/en-us/library/ff367988%28v=EXCHG.80%29.aspx

    eg

    $tmTemplateEmail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
    $tmTemplateEmail.ItemClass = "IPM.Note.Rules.ReplyTemplate.Microsoft";
    $tmTemplateEmail.IsAssociated = $true;
    $tmTemplateEmail.Subject = "Recipient of your Email";
    $htmlBodyString = "Hello,<p>Thanks for you Email our business hours are 9-5pm for after hours emergencies please contact 555";
    $tmTemplateEmail.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody($htmlBodyString);
    $PidTagReplyTemplateId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x65C2, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
    $tmTemplateEmail.SetExtendedProperty($PidTagReplyTemplateId, [System.Guid]::NewGuid().ToByteArray());
    $tmTemplateEmail.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox);
    $nrNewInboxRule = New-Object Microsoft.Exchange.WebServices.Data.Rule 
    $nrNewInboxRule.DisplayName = "Auto Reply Rule";
    $nrNewInboxRule.Actions.ServerReplyWithMessage = $tmTemplateEmail.Id;
    $cnCreateNewRule = New-Object Microsoft.Exchange.WebServices.Data.createRuleOperation[] 1
    $cnCreateNewRule[0] = $nrNewInboxRule
    $service.UpdateInboxRules($cnCreateNewRule,$true);
    Cheers
    Glen
    • Marked as answer by klingan Thursday, November 1, 2012 9:02 AM
    Thursday, November 1, 2012 5:22 AM

All replies

  • You need to set the PidTagReplyTemplateId property on the Template messages as per http://msdn.microsoft.com/en-us/library/ff367988%28v=EXCHG.80%29.aspx

    eg

    $tmTemplateEmail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
    $tmTemplateEmail.ItemClass = "IPM.Note.Rules.ReplyTemplate.Microsoft";
    $tmTemplateEmail.IsAssociated = $true;
    $tmTemplateEmail.Subject = "Recipient of your Email";
    $htmlBodyString = "Hello,<p>Thanks for you Email our business hours are 9-5pm for after hours emergencies please contact 555";
    $tmTemplateEmail.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody($htmlBodyString);
    $PidTagReplyTemplateId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x65C2, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
    $tmTemplateEmail.SetExtendedProperty($PidTagReplyTemplateId, [System.Guid]::NewGuid().ToByteArray());
    $tmTemplateEmail.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox);
    $nrNewInboxRule = New-Object Microsoft.Exchange.WebServices.Data.Rule 
    $nrNewInboxRule.DisplayName = "Auto Reply Rule";
    $nrNewInboxRule.Actions.ServerReplyWithMessage = $tmTemplateEmail.Id;
    $cnCreateNewRule = New-Object Microsoft.Exchange.WebServices.Data.createRuleOperation[] 1
    $cnCreateNewRule[0] = $nrNewInboxRule
    $service.UpdateInboxRules($cnCreateNewRule,$true);
    Cheers
    Glen
    • Marked as answer by klingan Thursday, November 1, 2012 9:02 AM
    Thursday, November 1, 2012 5:22 AM
  • Thank you!
    Thursday, November 1, 2012 9:02 AM