How to send deferred delivery email using Exchange Web Services ?

We can send deferred delivery mail using Exchange Web Services by setting PR_DEFERRED_SEND_TIME and PR_DEFERRED_DELIVERY_TIME MAPI properties as extended properties on the EWS request.

Here is the sample code in C#:

NOTE: Following programming examples is for illustration only, without warranty either expressed or implied,
including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose.
This sample code assumes that you are familiar with the programming language being demonstrated and the tools used
to create and debug procedures.

    1:  using System;
    2:  using System.Collections.Generic;
    3:  using System.Linq;
    4:  using System.Text;
    5:  using SendMailWithDeferredDelivery.msgex07;
    6:  using System.Net;
    7:  using System.Net.Security;
    8:  using System.Security.Cryptography.X509Certificates;
    9:   
   10:  namespace SendMailWithDeferredDelivery
   11:  {
   12:      class Program
   13:      {
   14:          static void Main(string[] args)
   15:          {
   16:              //Please change the server, domain, username, password 
   17:              ExchangeServiceBinding esb = new ExchangeServiceBinding();
   18:              esb.Credentials = new NetworkCredential("user", "pass!", "domain");
   19:              esb.Url = @"https://<FQDN Server>/EWS/Exchange.asmx";
   20:              
   21:   
   22:              System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
   23:              {
   24:                  // Replace this line with code to validate server certificate.
   25:                  return true;
   26:              };
   27:   
   28:   
   29:              esb.RequestServerVersionValue = new RequestServerVersion();
   30:              esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
   31:   
   32:              // Create a new message.
   33:              MessageType message = new MessageType();
   34:   
   35:              // Define the e-mail address of the recipient of the message.
   36:              EmailAddressType singleRecip = new EmailAddressType();
   37:              singleRecip.EmailAddress = "user@msglab.com";
   38:   
   39:              message.ToRecipients = new EmailAddressType[2];
   40:              message.ToRecipients[0] = singleRecip;
   41:   
   42:              // Set the subject and sensitivity properties.
   43:              message.Subject = "Siberian Tigers";
   44:              message.Sensitivity = SensitivityChoicesType.Personal;
   45:   
   46:              message.ExtendedProperty = new ExtendedPropertyType[2];
   47:   
   48:              message.ExtendedProperty[0] = new ExtendedPropertyType();
   49:   
   50:              message.ExtendedProperty[0].ExtendedFieldURI = new PathToExtendedFieldType();
   51:   
   52:              //NOTE: This PropertyID is equal to PropertyTag "0x3FEF" but you have to use PropertyID and not PropertyTag 
   53:              //otherwise it will NOT work as expected and this will not be visible via OOM or WebDAV, 
   54:              //you have to specify it as Integer value in PropertyTag field 
   55:              //PR_DEFERRED_SEND_TIME
   56:              message.ExtendedProperty[0].ExtendedFieldURI.PropertyTag = "16367";
   57:              
   58:              message.ExtendedProperty[0].ExtendedFieldURI.PropertyType = MapiPropertyTypeType.SystemTime;
   59:   
   60:              //Sending mail in another 5 Mins
   61:              DateTime deliverydatetime;
   62:              System.DateTime today = System.DateTime.Now;
   63:              System.TimeSpan duration = new System.TimeSpan(0,5, 0);
   64:              deliverydatetime = today.Add(duration);
   65:   
   66:              message.ExtendedProperty[0].Item = deliverydatetime.ToUniversalTime().ToString();
   67:   
   68:              //NOTE: This PropertyID is equal to PropertyTag "0x000F" but you have to use PropertyID and not PropertyTag 
   69:              //otherwise it will NOT work as expected and this will not be visible via OOM or WebDAV, 
   70:              //you have to specify it as Integer value in PropertyTag field 
   71:              //PR_DEFERRED_DELIVERY_TIME
   72:           
   73:              message.ExtendedProperty[1] = new ExtendedPropertyType();
   74:   
   75:              message.ExtendedProperty[1].ExtendedFieldURI = new PathToExtendedFieldType();
   76:   
   77:              message.ExtendedProperty[1].ExtendedFieldURI.PropertyTag = "15";
   78:             
   79:              //message.ExtendedProperty[1].ExtendedFieldURI.PropertyTag= "0x000F";
   80:   
   81:              message.ExtendedProperty[1].ExtendedFieldURI.PropertyType = MapiPropertyTypeType.SystemTime;
   82:   
   83:              message.ExtendedProperty[1].Item = deliverydatetime.ToUniversalTime().ToString();
   84:   
   85:               
   86:              // Set the body property.
   87:              message.Body = new BodyType();
   88:              message.Body.BodyType1 = BodyTypeType.HTML;
   89:              message.Body.Value =  "Did you know there are estimated to be only about 400 " +
   90:                                      "of these magnificent creatures left in the wild?";
   91:   
   92:              // Create a CreateItem request.
   93:              CreateItemType createItem = new CreateItemType();
   94:   
   95:              // Specify that the CreateItem operation should directly send the message
   96:              // and save a copy in the Sent Items folder.
   97:              createItem.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
   98:              createItem.MessageDispositionSpecified = true;
   99:   
  100:              DistinguishedFolderIdType folder = new DistinguishedFolderIdType();
  101:              folder.Id = DistinguishedFolderIdNameType.outbox;
  102:   
  103:              TargetFolderIdType targetFolder = new TargetFolderIdType();
  104:              targetFolder.Item = folder;
  105:              createItem.SavedItemFolderId = targetFolder;
  106:   
  107:              createItem.Items = new NonEmptyArrayOfAllItemsType();
  108:              createItem.Items.Items = new ItemType[1] { message };
  109:   
  110:              // Call the CreateItem method and get its response. 
  111:              CreateItemResponseType response = esb.CreateItem(createItem);
  112:   
  113:              // Get the items returned by CreateItem.
  114:              ResponseMessageType[] itemsResp = response.ResponseMessages.Items;
  115:              
  116:          }
  117:      }
  118:  }
  
 EWS Rocks!!!

Comments