How to: Delay sending an email message by using the EWS Managed API 2.0
Learn how to delay sending an email message by using the EWS Managed API.
Last modified: July 01, 2013
Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010
Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.
In this article
Get started delaying email messages
Delay sending an email message
Next steps
Additional resources
Exchange 2013: Send delayed emails programmatically on Exchange servers
Get started delaying email messages
To get started using the EWS Managed API to delay sending email messages, complete the following tasks:
In Visual Studio, add a reference to the EWS Managed API.
Ensure that you have access to an active Office 365, Exchange Online, Exchange 2013, Exchange 2010, or Exchange 2007 mailbox.
Ensure that you have an ExchangeService object that has the correct credentials and the Exchange Web Services (EWS) URL. For information about setting up the ExchangeService object, see Connecting to EWS by using the EWS Managed API 2.0.
Delay sending an email message
The following code example shows how to delay sending an email message. To use this code example, update the SMTP address in the To: recipients line.
// Delays the time that an email message is sent.
static void DelaySendEmail(ExchangeService service)
{
EmailMessage message = new EmailMessage(service);
// Identify the extended properties that are used to set the time when the email message is sent.
ExtendedPropertyDefinition PR_DEFERRED_SEND_TIME = new ExtendedPropertyDefinition(16367,
MapiPropertyType.SystemTime);
// Identify when the email message will be sent and delivered. The email message is delivered three minutes after the
// next line executes.
string sendTime = DateTime.Now.AddMinutes(2).ToUniversalTime().ToString();
// Set the extended properties with the dateTime for when the mail will be delivered.
message.SetExtendedProperty(PR_DEFERRED_SEND_TIME, sendTime);
message.ToRecipients.Add("tadams@contoso.com");
message.Subject = "Test subject";
StringBuilder str = new StringBuilder();
str.AppendLine("Client submitted at: " + DateTime.Now.ToString());
Console.WriteLine("Client submitted at: " + DateTime.Now.ToString());
str.AppendLine("To be sent at: " + sendTime);
Console.WriteLine("To be sent at: " + sendTime);
message.Body = str.ToString();
message.SendAndSaveCopy();
}
This code example first creates an email message object, and then it creates an extended property definition to specify the MAPI property that is used to set the deferred send time. For more information about this property, see PidTagDeferredSendTime Canonical Property. The deferred send time information is set on the email message along with an email recipient, a subject, and the email message body. 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.
Next steps
This article shows how to delay sending an email message to a single recipient. Next, you might want to explore other ways that you can use the EWS Managed API to work with email messages. See the following articles: