# Schedule Send

The **Email SDK** supports scheduling draft email messages to be sent at a specified time in the future. The Email Service will periodically check for messages that have been scheduled whose `sendAt` times have passed and will send them as normal. Your runtime instance can be configured to specify how often you want this check to occur. More frequent checks will increase the chances that the messages will be sent as close to the specified time as possible. If a message fails to send for any reason, it may also be retried a configurable number of times.

### Scheduling a Draft Message

To schedule a draft message to be sent in the future, call the `scheduleSendDraftMessage` method. This method takes in the `id` of the draft message to schedule, the `emailAddressId` of the email address that owns the draft message and the `sendAt` timestamp of when to send the message. The call will return a record of the scheduled message with useful information including the `sendAt` timestamp, the `updatedAt` timestamp and the `state` of the scheduled message. There are four states that the scheduled message may be in:

<table><thead><tr><th width="185.20703125">State</th><th>Meaning</th></tr></thead><tbody><tr><td>SCHEDULED</td><td>The message has been scheduled and will be sent once the <code>sendAt</code> time has passed.</td></tr><tr><td>FAILED</td><td>The message failed to send after the <code>sendAt</code> passed.</td></tr><tr><td>SENT</td><td>The message has successfully been sent. It should appear in the <code>SENT</code> folder of the user's email address.</td></tr><tr><td>CANCELLED</td><td>The message was cancelled and will not be sent.</td></tr></tbody></table>

{% tabs %}
{% tab title="TypeScript" %}

```typescript
// Obtain the draftId and emailAddressId however makes sense for your application
const id = draft.id
const emailAddressId = senderEmailAddress.id
const sendAt = DateTime.now().plus({ day: 1 }).toJSDate() // Adjust this to the required time in the future
try {
    const scheduledDraftMessage = await emailClient.scheduleSendDraftMessage({
        id,
        emailAddressId,
        sendAt,
    })
} catch (e) {
    // Handle/notify user of errors
}
```

{% endtab %}

{% tab title="Swift" %}

```swift
// Obtain the draftId and emailAddressId however makes sense for your application
let id = draft.id
let emailAddressId = senderEmailAddress.id
let sendAt = Date().addingTimeInterval(+86400) // Adjust this to the required time in the future
let input = ScheduleSendDraftMessageInput(
    id: id,
    emailAddressId: emailAddressId,
    sendAt: sendAt
)
do {
    let scheduledDraftMessage = try await emailClient.scheduleSendDraftMessage(withInput: input)
} catch {
    // Handle/notify user of errors
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
// Obtain the draftId and emailAddressId however makes sense for your application
val id = draft.id
val emailAddressId = senderEmailAddress.id
val sendAt = Date(Date().time + Duration.ofDays(1).toMillis()) // Adjust this to the required time in the future
launch {
    try {
        val input = ScheduleSendDraftMessageInput(
            id,
            emailAddressId,
            sendAt,
        )
        val scheduledDraftMessage = withContext(Dispatchers.IO) {
            emailClient.scheduleSendDraftMessage(input)
        }
    } catch (e: EmailMessageException) {
        // Handle/notify user of exception
    }
}
```

{% endtab %}
{% endtabs %}

### Cancelling a Scheduled Draft Message

A previously scheduled draft message can be cancelled at any time before it's specified `sendAt` time by calling the `cancelScheduledDraftMessage` method. This method takes the draft message `id` and the `emailAddressId` of the email address that owns the draft and will return the draft `id` on successful cancellation.

{% tabs %}
{% tab title="TypeScript" %}

```typescript
// Obtain the draftId and emailAddressId however makes sense for your application
const id = draft.id
const emailAddressId = senderEmailAddress.id
try {
    const cancelledScheduledDraftId = await client.cancellScheduledDraftMessage({
        id,
        emailAddressId,
    })
} catch (e) {
    // Handle/notify user of errors
}
```

{% endtab %}

{% tab title="Swift" %}

```swift
// Obtain the draftId and emailAddressId however makes sense for your application
let id = draft.id
let emailAddressId = senderEmailAddress.id
let input = CancelScheduledDraftMessageInput(
    id: id,
    emailAddressId: emailAddressId,
)
do {
    let cancelledScheduledDraftId = try await emailClient.cancelScheduledDraftMessage(withInput: input)
} catch {
    // Handle/notify user of errors
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
// Obtain the draftId and emailAddressId however makes sense for your application
val id = draft.id
val emailAddressId = senderEmailAddress.id
launch {
    try {
        val input = CancelScheduledDraftMessageInput(
            id,
            emailAddressId,
        )
        val cancelledScheduledDraftId = withContext(Dispatchers.IO) {
            emailClient.cancelScheduledDraftMessage(input)
        }
    } catch (e: EmailMessageException) {
        // Handle/notify user of exception
    }
}
```

{% endtab %}
{% endtabs %}

### Listing Scheduled Draft Messages

To retrieve a list of previously scheduled draft messages, call the `listScheduledDraftMessagesForEmailAddressId` method. This method takes the `emailAddressId` , an optional `filters` argument to filter your results and pagination arguments (See [Pagination](/guides/email/pagination.md) for details). It will return a list of Scheduled Draft Messages.

{% tabs %}
{% tab title="TypeScript" %}

```typescript
// Obtain the emailAddressId however makes sense for your application
const emailAddressId = senderEmailAddress.id
const filter: ScheduledDraftMessageFilterInput = {
    state: {
        notEqual: ScheduledDraftMessageState.CANCELLED,
    },
}
try {
    const listResult = await emailClient.listScheduledDraftMessagesForEmailAddressId({
        emailAddressId,
        filter,
    })
} catch (e) {
    // Handle/notify user of errors
}
```

{% endtab %}

{% tab title="Swift" %}

```swift
// Obtain the emailAddressId however makes sense for your application
let emailAddressId = senderEmailAddress.id
let input = ListScheduledDraftMessagesForEmailAddressIdInput(
    emailAddressId: emailAddressId,
    limit: nil,
    nextToken: nil,
    filter: ScheduledDraftMessageFilter(
        state: ScheduledDraftMessageStateFilter.notEqual(.cancelled)
    )
)
do {
    let listResult = try await emailClient.listScheduledDraftMessagesForEmailAddressId(withInput: input)
} catch {
    // Handle/notify user of errors
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
// Obtain the draftId and emailAddressId however makes sense for your application
val emailAddressId = senderEmailAddress.id
launch {
    try {
        vval input = ListScheduledDraftMessagesForEmailAddressIdInput(
            emailAddressId,
            filter = ScheduledDraftMessageFilterInput(
                state = NotEqualStateFilter(
                    notEqual = ScheduledDraftMessageState.CANCELLED,
                ),
            ),
        )
        val listResult = withContext(Dispatchers.IO) {
            emailClient.listScheduledDraftMessagesForEmailAddressId(input)
        }
    } catch (e: EmailMessageException) {
        // Handle/notify user of exception
    }
}
```

{% endtab %}
{% endtabs %}

The `listScheduledDraftMessagesForEmailAddressId` method currently supports filtering results by `state`. The above example would list all results where the `state` is not equal to `CANCELLED` , but there are other options as defined below:

<table><thead><tr><th width="112.578125">Filter Option</th><th width="320.21484375">Type</th><th>Meaning</th></tr></thead><tbody><tr><td><code>equal</code></td><td><code>ScheduledDraftMessageState</code></td><td>Return only results that match the given state.</td></tr><tr><td><code>oneOf</code></td><td><code>Array&#x3C;ScheduledDraftMessageState></code></td><td>Return only results that match one of the given states.</td></tr><tr><td><code>notEqual</code></td><td><code>ScheduledDraftMessageState</code></td><td>Return only results that do not match the given state.</td></tr><tr><td><code>notOneOf</code></td><td><code>Array&#x3C;ScheduledDraftMessageState></code></td><td>Return only results that do not match any of the given states.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sudoplatform.com/guides/email/schedule-send.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
