Allow your users the flexibility to manage their inbox with email folders and save draft emails securely
When an email address is provisioned through the Email SDK, a set of standard folders are also created and associated with the provisioned email address. The following table describes the standard folders:
Folder
Function
INBOX
Inbound messages are assigned to the INBOX folder.
SENT
Outbound messages which have completed sending are assigned to the SENT folder.
OUTBOX
Reserved for future use.
TRASH
Messages can be moved to the TRASH folder (this does not permanently delete the message).
Retrieving Email Folders
Email folders associated with an email address are available in the folders attribute of that email address and are retrieved along with the email address.
Email folders associated with an email address can also be retrieved directly through the listEmailFoldersForEmailAddressId method by passing in the id of the email address to query. This returns a ListOutput object with the list of email folder objects which contain information used to identify each folder and to facilitate moving of messages between folders.
The EmailFolder object contains useful information such as the name of the folder, the total size of all messages assigned to the folder and the total count of unseen messages assigned to the folder.
// Obtain the input email address id however makes sense for your implementation.constemailAddressId=emailAddress.idtry {constlistOutput=awaitemailClient.listEmailFoldersForEmailAddressId({ emailAddressId, cachePolicy:CachePolicy.RemoteOnly, nextToken, })// `listOutput` contains the list of items matching the input. // Page through the results if listOutput.nextToken != nil.} catch (e) {// Handle/notify user of errors}
// Obtain the input email address id however makes sense for your implementation.let emailAddressId = emailAddress.idlet input =ListEmailFoldersForEmailAddressIdInput( emailAddressId: emailAddressId, cachePolicy: .remoteOnly, limit:10, nextToken:nil)do {let output =tryawait emailClient.listEmailFoldersForEmailAddressId( withInput: input)// `output` contains the `ListOutput` of items matching the input.// `output.items` contains an array of `EmailFolder`// Page through the results if output.nextToken != undefined.} catch {// Handle/notify user of errors}
// Obtain the input email address id however makes sense for your implementation.val emailAddressId = emailAddress.idlaunch {try {val input =ListEmailFoldersForEmailAddressIdInput( emailAddressId = emailAddressId, limit =10, nextToken =null )val listOutput =withContext(Dispatchers.IO) { emailClient.listEmailFoldersForEmailAddressId(input) }// [listOutput] contains the [ListOutput] of items matching the input.// [listOutput.items] contains a list of [EmailFolder]s// Page through the results if listOutput.nextToken != null. } catch (e: EmailMessageException) {// Handle/notify user of exception }}
Move Email Messages between Folders
Email messages can also be moved between folders. For example an email message stored in the "inbox" folder can be moved to the "trash" folder. This is done through the update email messages API as described here. The email message object contains a previousFolderId property which specifies the identifier of the folder that the message was previously in. An example implementation of using the updateEmailMessages method to move a message to another folder is:
// Obtain the input message ids and email address id however makes sense for your implementation.constemailAddressId=emailAddress.idconstids= ['message-id-1','message-id-2']try {// List the email folders and find the "trash" folder.consttrashFolder=awaitemailClient.listEmailFoldersForEmailAddressId({ emailAddressId, cachePolicy:CachePolicy.RemoteOnly }).then((result) =>result.items.find(folder) =>folder.folderName ==='TRASH')// Update an email message using the "trash" folder id.awaitemailClient.updateEmailMessages({ ids, values: { folderId:trashFolder.id }, })// Each of the input messages should now be assigned to the "trash" folder.} catch (e) {// Handle/notify user of errors}
// Obtain the input message ids and email address id however makes sense for your implementation.let emailAddressId = emailAddress.idlet ids = ['message-id-1', 'message-id-2']let input =ListEmailFoldersForEmailAddressIdInput( emailAddressId: emailAddressId, cachePolicy: .remoteOnly, limit:10, nextToken:nil)do {// List the email folders and find the "trash" folder.let output =tryawait emailClient.listEmailFoldersForEmailAddressId( withInput: input)let trashFolderIds = output.items.filter { $0.id.contains("TRASH") }guardlet trashFolderId = trashFolderIds.firstelse {return }let input =UpdateEmailMessagesInput( ids: ids, values: UpdateEmailMessagesValues( folderId: trashFolderId, seen:true))// Update the email messages using the "trash" folder id.let result =tryawait emailClient.updateEmailMessages(withInput: input)switch result {case .success:// All email messages assigned to the TRASH folder.case. partial:// `result.successItems` contains an array of all the email message IDs // that were assigned to the TRASH folder successfully.// `result.failureItems` cotains an array of all the email message IDs// that failed to be assigned to the TRASH folder.case .failure:// All email messages failed to be assigned to the TRASH folder. }} catch {// Handle/notify user of errors}
// Obtain the input draft message ids and email address id however makes sense for your implementation.val emailAddressId = emailAddress.idval ids =listOf("message-id-1", "message-id-2")launch {try {// List the email folders and find the "trash" folder.val listInput =ListEmailFoldersForEmailAddressIdInput( emailAddressId = emailAddressId, limit =10, nextToken =null )val emailFoldersList =withContext(Dispatchers.IO) { emailClient.listEmailFoldersForEmailAddressId(listInput) }val trashFolder = (emailFoldersList.filter { it.folderName =="TRASH" }).first()// Update the email messages using the "trash" folder id.val updateInput =UpdateEmailMessagesInput( ids = ids, values = UpdateEmailMessagesInput.UpdatableValues(folderId = trashFolder.id) )val result =withContext(Dispatchers.IO) { emailClient.updateEmailMessages(updateInput) }when (result) {is BatchOperationResult.SuccessOrFailureResult -> {// [result.status] contains the status of the batch update operation. }is BatchOperationResult.Partial -> {// [result.successValues] contains the list of items in which the update operation succeeded.// [result.failureValues] contains the list of items in which the update operation failed. } } } catch (e: EmailMessageException) {// Handle/notify user of exception }}
Draft Email Messages
The Email SDK also supports the notion of draft email messages. A separate set of APIs are available to handle the lifecycle of drafts.
Creating a Draft Email Message
To create and save a draft email message, call the createDraftEmailMessage method. This method takes in a rfc822Data input property which contains the email message content formatted under RFC 822 as well as the senderEmailAddressId input property which must match the identifier of the email addess from the from field in the RFC 822 data. A call to this method returns metadata of the saved draft. Draft email message data provides the ID of the draft and an updatedAt timestamp representing the last time the draft was saved.
// Obtain the input RFC 822 data and sender email address id however makes sense for your implementation.constrfc822Data:ArrayBuffer=// ...constsenderEmailAddressId=senderEmailAddress.idtry {constdraftMetadata=awaitemailClient.createDraftEmailMessage({ rfc822Data, senderEmailAddressId, })// `draftMetadata` is the metadata associated with the saved// draft email message including the id you can use this to access or// update the draft.} catch (e) {// Handle/notify user of errors}
// Obtain the input RFC 822 data and sender email address id however makes sense for your implementation.let rfc822Data: Data =// ...let senderEmailAddressId = senderEmailAddress.idlet input =CreateDraftEmailMessageInput( rfc822Data: rfc822Data, senderEmailAddressId: senderEmailAddressId)do {let draftMetadata =tryawait emailClient.createDraftEmailMessage( withInput: input)// `draftMetadata` is the `DraftEmailMessageMetadata` associated with the saved// draft email message including the id which you can use to access or// update the draft.} catch {// Handle/notify user of errors}
// Obtain the input RFC 822 data and sender email address id however makes sense for your implementation.val rfc822Data: ByteArray=// ...val senderEmailAddressId = senderEmailAddress.idlaunch {try {val input =CreateDraftEmailMessageInput( rfc822Data = rfc822Data, senderEmailAddressId = senderEmailAddressId )val draftId =withContext(Dispatchers.IO) { emailClient.createDraftEmailMessage(input) }// [draftId] is the unique id of the draft that you can use to access // or update the draft } catch (e: EmailMessageException) {// Handle/notify user of exception }}
Updating a Draft Email Message
Use the updateDraftEmailMessage method to update a previously created and saved draft email message. The input requires the id of the previously saved draft to perform the update. It is a requirement that the entire message content be replaced with an updated version as part of the rfc822Data input property. A call to this method returns the id of the draft that was updated which should match the id provided in the input.
// Obtain the input RFC 822 data, sender email address id and existing draft id however makes sense for your implementation.
constid:string=// ...constrfc822Data:ArrayBuffer=// ...constsenderEmailAddressId=senderEmailAddress.id try {constdraftMetadata=awaitemailClient.updateDraftEmailMessage({ id, rfc822Data, senderEmailAddressId, })// `draftMetadata` is the metadata associated with the saved// draft email message including the id you can use this to access or// update the draft.} catch (e) {// Handle/notify user of errors}
// Obtain the input RFC 822 data, sender email address id and existing draft id however makes sense for your implementation.
let id: String=// ...let rfc822Data: Data =// ...let senderEmailAddressId = senderEmailAddress.idlet input =UpdateDraftEmailMessageInput( id: id, rfc822Data: rfc822Data, senderEmailAddressId: senderEmailAddressId)do {let draftMetadata =tryawait emailClient.updateDraftEmailMessage( withInput: input)// `draftMetadata` is the `DraftEmailMessageMetadata` associated with the// updated draft email message including the id which you can use to access// or update the draft.} catch {// Handle/notify user of errors}
// Obtain the input RFC 822 data, sender email address id and existing draft id however makes sense for your implementation.
val id: String=// ...val rfc822Data: ByteArray=// ...val senderEmailAddressId = senderEmailAddress.idlaunch {try {val input =UpdateDraftEmailMessageInput( id = id, rfc822Data = rfc822Data, senderEmailAddressId = senderEmailAddressId )val draftId =withContext(Dispatchers.IO) { emailClient.updateDraftEmailMessage(input) }// [draftId] is the unique id of the draft that you can use to access // or update the draft } catch (e: EmailMessageException) {// Handle/notify user of exception }}
Deleting Draft Email Messages
Draft email messages can be deleted in batches using the deleteDraftEmailMessages method by passing in one or more draft message identifiers and the id of the email address associated with the drafts.
Draft email messages that have been deleted will no longer be available and all traces of the message data and metadata will be deleted permanently.
Draft email messages can only be deleted in batches of 10. Supplying identifiers to the input which exceed this limit will return a LimitExceededError.
A BatchOperationResult type is returned from this method call which contains the status of the batch delete operation. Three possible statuses can be returned:
Status
Definition
Success
All of the draft email messages succeeded to delete. The return object will include a list of identifiers of messages which succeeded to delete.
Partial
Only a subset of draft email messages succeeded to delete. The return object will include a list of identifiers and error descriptions of draft email messages which failed the delete and a list which succeeded the delete.
Failure
All of the draft email messages failed to delete.
// Obtain the input draft message ids and email address id however makes sense for your implementation.constids=arrayOf('draft-msg-id-1','draft-msg-id-2')constemailAddressId=emailAddress.idtry {constresult=awaitemailClient.deleteDraftEmailMessages({ ids, emailAddressId, })// `result` contains the status of the batch delete operation and associated success and failure metadata.} catch (e) {// Handle/notify user of errors}
// Obtain the input draft message ids and email address id however makes sense for your implementation.let ids = ['draft-msg-id-1', 'draft-msg-id-2']let emailAddressId = emailAddress.idlet input =DeleteDraftEmailMessagesInput( ids: ids, emailAddressId: emailAddressId)do {let result =tryawait self.emailClient.deleteDraftEmailMessages( withInput: input)// `result` contains the status of the batch delete operation and associated success and failure metadata.} catch {/// Handle/notify user of error}
// Obtain the input draft message ids and email address id however makes sense for your implementation.val ids =listOf("draft-msg-id-1", "draft-msg-id-2")val emailAddressId = emailAddress.idlaunch {try {val input =DeleteDraftEmailMessagesInput( ids = ids, emailAddressId = emailAddressId )val result =withContext(Dispatchers.IO) { emailClient.deleteDraftEmailMessages(input) }// [result] contains the status of the batch delete operation and associated success and failure metadata. } catch (e: EmailMessageException) {// Handle/notify user of exception }}
Retrieving Draft Email Messages
Draft email messages can be accessed using its identifier or by querying for a list of its identifiers.
If a draft email message has been deleted, it will no longer be available for access.
Single Draft Email Message by Id
To retrieve a single draft email message, use the getDraftEmailMessage method. This method takes in the id of the draft to retrieve as well as the id of the email address that is associated with the draft. This method returns the draft email message data if it exists.
// Obtain the input email address id and draft id however makes sense for// your implementation.constemailAddressId=emailAddress.idtry {constdraftMessage=awaitemailClient.getDraftEmailMessage({ id, emailAddressId, })// `draftMessage` containing draft message data and `updatedAt` timestamp// will be returned if a draft message corresponding to `id` is found,// else `undefined`.} catch (e) {// Handle/notify user of errors}
// Obtain the input draft message id and email address id however makes sense for your implementation.let id = 'draft-msg-id-1'let emailAddressId = emailAddress.idlet input =GetDraftEmailMessageInput( id: id, emailAddressId: emailAddressId)do {let draftEmailMessage =tryawait emailClient.getDraftEmailMessage( withInput: input)// `draftMessage` containing draft message data and `updatedAt` timestamp// will be returned if a draft message corresponding to `id` is found,// else `nil`.} catch {// Handle/notify user of errors}
// Obtain the input draft message id and email address id however makes sense for your implementation.val id ="draft-msg-id-1"val emailAddressId = emailAddress.idlaunch {try {val input =GetDraftEmailMessageInput( id = id, emailAddressId = emailAddressId )val draftMessage =withContext(Dispatchers.IO) { emailClient.getDraftEmailMessage(input) }// [draftMessage] containing draft message data and [updatedAt] timestamp// will be returned if a draft message corresponding to [id] is found,// else will throw an exception. } catch (e: EmailMessageException) {// Handle/notify user of exception }}
List of All Draft Email Messages
To retrieve a list of all draft email messages associated with a user, call the listDraftEmailMessages method.
try {constresult=awaitemailClient.listDraftEmailMessages()// `result` contains a list of draft messages consisting of the // draft message data and the `updatedAt` timestamp.} catch {// Handle/notify user of errors}
do {let result =tryawait emailClient.listDraftEmailMessages()// `result` contains a list of draft messages consisting of the // draft message data and the `updatedAt` timestamp.} catch {// Handle/notify user of errors}
launch {try {val result =withContext(Dispatchers.IO) { emailClient.listDraftEmailMessages() }// [result] contains a list of draft messages consisting of the // draft message data and the [updatedAt] timestamp. } catch (e: EmailMessageException) {// Handle/notify user of exception }}
List of Draft Email Messages for an Email Address
To retrieve a list of multiple draft email messages associated with a certain email address, call the listDraftEmailMessagesForEmailAddressId method by passing in the id of the email address to query.
// Obtain the input email address id however makes sense for your implementation.constemailAddressId=emailAddress.idtry {constresult=awaitemailClient.listDraftEmailMessagesForEmailAddressId( emailAddressId )// `result` contains a list of draft messages consisting of the // draft message data and the `updatedAt` timestamp.} catch {// Handle/notify user of errors}
// Obtain the input email address id however makes sense for your implementation.let emailAddressId = emailAddress.iddo {let result =tryawait emailClient.listDraftEmailMessagesForEmailAddressId( emailAddressId)// `result` contains a list of draft messages consisting of the // draft message data and the `updatedAt` timestamp.} catch {// Handle/notify user of errors}
// Obtain the input email address id however makes sense for your implementation.val emailAddressId = emailAddress.idlaunch {try {val result =withContext(Dispatchers.IO) { emailClient.listDraftEmailMessagesForEmailAddressId(emailAddressId) }// [result] contains a list of draft messages consisting of the // draft message data and the [updatedAt] timestamp. } catch (e: EmailMessageException) {// Handle/notify user of exception }}
List of All Draft Email Message Metadata
Use listDraftEmailMessageMetadata to retrieve a list of all the metadata of the draft email messages associated with a user. The metadata returned in the list can be used to retrieve individual draft email message content, perform an update to an already saved draft, or identify which drafts to perform full retrieval of based on updatedAt timestamp.
try {constresult=awaitemailClient.listDraftEmailMessageMetadata()// `result` contains a list of all of the draft email messages'// metadata.} catch (e) {// Handle/notify user of errors}
do {let result =tryawait emailClient.listDraftEmailMessageMetadata()// `result` contains an array of `DraftEmailMessageMetadata` of all of the// draft email messages' metadata.} catch {// Handle/notify user of errors}
launch {try {val result =withContext(Dispatchers.IO) { emailClient.listDraftEmailMessageMetadata() }// [result] contains a list of [DraftEmailMessageMetadata] of all of the// draft email messages' metadata. } catch (e: EmailMessageException) {// Handle/notify user of exception }}
List of Draft Email Message Metadata for an Email Address
To retrieve a list of all the metadata of the draft messages associated with an email address, call the listDraftEmailMessageMetadataForEmailAddressId method by passing in the id of the email address to query.
// Obtain the input email address id however makes sense for your implementation.constemailAddressId=emailAddress.idtry {constresult=awaitemailClient.listDraftEmailMessageMetadataForEmailAddressId( emailAddressId )// `result` contains a list of all of the draft email messages'// metadata.} catch (e) {// Handle/notify user of errors}
// Obtain the input email address id however makes sense for your implementation.let emailAddressId = emailAddress.idtry {let result =tryawait emailClient.listDraftEmailMessageMetadataForEmailAddressId( emailAddressId)// `result` contains an array of `DraftEmailMessageMetadata` of all of the// draft email messages' metadata.} catch {// Handle/notify user of errors}
// Obtain the input email address id however makes sense for your implementation.val emailAddressId = emailAddress.idlaunch {try {val result =withContext(Dispatchers.IO) { emailClient.listDraftEmailMessageMetadataForEmailAddressId(emailAddressId) }// [result] contains a list of [DraftEmailMessageMetadata] of all of the// draft email messages' metadata. } catch (e: EmailMessageException) {// Handle/notify user of exception }}