DataFlex
DataFlex
Outlook -- Create Reply Email, Update, and Send
See more Outlook Examples
Creates a reply email in the Drafts folder, updates the reply with information, and then sends the reply.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Handle hoHtFolderMap
Variant vSbMap
Handle hoSbMap
String sFolderId
Handle hoJson
Variant vSbResponse
Handle hoSbResponse
String sExistingMsgId
Variant vJsonRequestBody
Handle hoJsonRequestBody
Variant vResp
Handle hoResp
Handle hoJsonResponse
String sReplyMsgId
Variant vJsonPatch
Handle hoJsonPatch
Variant vSbHtml
Handle hoSbHtml
Integer iNumReplaced
Integer iNumToRecipients
Integer i
Integer iNumCcRecipients
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// Use your previously obtained access token here:
Set ComAuthToken Of hoHttp To "MICROSOFT_GRAPH_ACCESS_TOKEN"
// This example will search /Inbox for a message that will be replied to.
// First we need to get the folder ID for /Inbox.
// Then we'll search for messages based on some criteria, and reply to the 1st matching message.
// To reply, we'll create the reply message in the Drafts folder, update it with content, and the send.
// Get the folder ID for /Inbox from the folder map created by this example
Get Create (RefClass(cComChilkatHashtable)) To hoHtFolderMap
If (Not(IsComObjectCreated(hoHtFolderMap))) Begin
Send CreateComObject of hoHtFolderMap
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbMap
If (Not(IsComObjectCreated(hoSbMap))) Begin
Send CreateComObject of hoSbMap
End
Get ComLoadFile Of hoSbMap "qa_data/outlook/folderMap.xml" "utf-8" To iSuccess
Get pvComObject of hoSbMap to vSbMap
Get ComAddFromXmlSb Of hoHtFolderMap vSbMap To iSuccess
// Get the ID for the "/Inbox" folder:
Get ComLookupStr Of hoHtFolderMap "/Inbox" To sFolderId
Get ComLastMethodSuccess Of hoHtFolderMap To bTemp1
If (bTemp1 <> True) Begin
Showln "Folder ID not found"
Procedure_Return
End
Move True To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Set ComEmitCompact Of hoJson To False
// Search for unread emails in this folder from support@chilkatsoft.com
Get ComSetUrlVar Of hoHttp "folder_id" sFolderId To iSuccess
Get ComSetUrlVar Of hoHttp "select" "id,subject" To iSuccess
Get ComSetUrlVar Of hoHttp "filter" "(isRead eq false) and (from/emailAddress/address eq 'support@chilkatsoft.com')" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
If (Not(IsComObjectCreated(hoSbResponse))) Begin
Send CreateComObject of hoSbResponse
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}" vSbResponse To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
// Show the results..
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
Get ComSizeOfArray Of hoJson "value" To iTemp1
If (iTemp1 = 0) Begin
Showln "Empty result set."
Procedure_Return
End
// Sample results:
// {
// "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA')/messages(id,subject)",
// "value": [
// {
// "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADOpwfq\"",
// "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM6Jj1wAAAA=",
// "subject": "This email contains a PDF attachment"
// },
// {
// "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADOpwfs\"",
// "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM6Jj14AAAA=",
// "subject": "This email has the word Amazon in the subject.."
// }
// ]
// }
// We'll create a reply for the 1st message in the result set.
// We'll need the message existing id.
Get ComStringOf Of hoJson "value[0].id" To sExistingMsgId
// To create a reply, send a request such as the following:
// POST https://graph.microsoft.com/v1.0/me/messages/{id}/createReply
// Content-type: application/json
// Content-length: 248
//
// {
// "comment": "comment-value"
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonRequestBody
If (Not(IsComObjectCreated(hoJsonRequestBody))) Begin
Send CreateComObject of hoJsonRequestBody
End
Get ComUpdateString Of hoJsonRequestBody "comment" "This is a comment" To iSuccess
Get ComSetUrlVar Of hoHttp "message_id" sExistingMsgId To iSuccess
// Create the reply in the Drafts folder:
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoJsonRequestBody to vJsonRequestBody
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" "https://graph.microsoft.com/v1.0/me/messages/{$message_id}/createReply" vJsonRequestBody "application/json" vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
// A 201 response indicates success.
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 201) Begin
Showln "Created reply draft."
Move True To iSuccess
End
Else Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Response status code = " iTemp1
Showln "Failed to create reply draft."
Move False To iSuccess
End
// Show the response in both cases..
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse
If (Not(IsComObjectCreated(hoJsonResponse))) Begin
Send CreateComObject of hoJsonResponse
End
Set ComEmitCompact Of hoJsonResponse To False
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoJsonResponse sTemp1 To iSuccess
Get ComEmit Of hoJsonResponse To sTemp1
Showln sTemp1
If (iSuccess = False) Begin
Procedure_Return
End
// ----------------------------------------------
// Get the message id of the newly created reply.
Get ComStringOf Of hoJsonResponse "id" To sReplyMsgId
// Update the message with new text in the body..
// Send an HTTP PATCH request that looks something like this:
// Only send the message parts that are changing.
// PATCH https://graph.microsoft.com/v1.0/me/messages/{reply_message_id}
// Content-type: application/json
// Content-length: 248
//
// {
// "subject": "subject-value",
// "body": {
// "contentType": "html",
// "content": "updated HTML goes here"
// },
// "inferenceClassification": "other"
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonPatch
If (Not(IsComObjectCreated(hoJsonPatch))) Begin
Send CreateComObject of hoJsonPatch
End
Get ComUpdateString Of hoJsonPatch "body.contentType" "html" To iSuccess
// The reply email that Outlook created will contain the original message under a horizontal rule.
// The body.content should contain this substring: "<body bgcolor=\"#FFFFFF\">\r\n<hr "
// We're going to insert our reply after the body tag, and before the hr tag.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHtml
If (Not(IsComObjectCreated(hoSbHtml))) Begin
Send CreateComObject of hoSbHtml
End
Get pvComObject of hoSbHtml to vSbHtml
Get ComStringOfSb Of hoJsonResponse "body.content" vSbHtml To iSuccess
// Insert the response HTML in the reply HTML body.
Get ComReplaceBetween Of hoSbHtml '<body bgcolor="#FFFFFF">' "<hr " (character(13)) + (character(10)) "<p>This is my response.</p>" To iNumReplaced
If (iNumReplaced <> 1) Begin
Showln "Something is amiss!"
Procedure_Return
End
Get ComGetAsString Of hoSbHtml To sTemp1
Get ComUpdateString Of hoJsonPatch "body.content" sTemp1 To iSuccess
// Add additional CC, BCC, or TO recipients like this:
Get ComSizeOfArray Of hoJsonResponse "toRecipients" To iNumToRecipients
Move 0 To i
// Copy existing TO recipients.
While (i < iNumToRecipients)
Set ComI Of hoJsonPatch To i
Set ComI Of hoJsonResponse To i
Get ComStringOf Of hoJsonResponse "toRecipients[i].emailAddress.name" To sTemp1
Get ComUpdateString Of hoJsonPatch "toRecipients[i].emailAddress.name" sTemp1 To iSuccess
Get ComStringOf Of hoJsonResponse "toRecipients[i].emailAddress.address" To sTemp1
Get ComUpdateString Of hoJsonPatch "toRecipients[i].emailAddress.address" sTemp1 To iSuccess
Move (i + 1) To i
Loop
// Add an additional TO recipient.
Set ComI Of hoJsonPatch To iNumToRecipients
Get ComUpdateString Of hoJsonPatch "toRecipients[i].emailAddress.name" "Chilkat" To iSuccess
Get ComUpdateString Of hoJsonPatch "toRecipients[i].emailAddress.address" "admin@chilkat.io" To iSuccess
Get ComSizeOfArray Of hoJsonResponse "ccRecipients" To iNumCcRecipients
Move 0 To i
// Copy existing CC recipients.
While (i < iNumCcRecipients)
Set ComI Of hoJsonPatch To i
Set ComI Of hoJsonResponse To i
Get ComStringOf Of hoJsonResponse "ccRecipients[i].emailAddress.name" To sTemp1
Get ComUpdateString Of hoJsonPatch "ccRecipients[i].emailAddress.name" sTemp1 To iSuccess
Get ComStringOf Of hoJsonResponse "ccRecipients[i].emailAddress.address" To sTemp1
Get ComUpdateString Of hoJsonPatch "ccRecipients[i].emailAddress.address" sTemp1 To iSuccess
Move (i + 1) To i
Loop
// Add an additional CC recipient.
Set ComI Of hoJsonPatch To iNumCcRecipients
Get ComUpdateString Of hoJsonPatch "ccRecipients[i].emailAddress.name" "Chilkat GMail" To iSuccess
Get ComUpdateString Of hoJsonPatch "ccRecipients[i].emailAddress.address" "chilkat.support@gmail.com" To iSuccess
Set ComEmitCompact Of hoJsonPatch To False
Get ComEmit Of hoJsonPatch To sTemp1
Showln sTemp1
Get ComSetUrlVar Of hoHttp "message_id" sReplyMsgId To iSuccess
Get pvComObject of hoJsonPatch to vJsonPatch
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "PATCH" "https://graph.microsoft.com/v1.0/me/messages/{$message_id}" vJsonPatch "application/json" vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
// A 200 response indicates success.
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 200) Begin
Showln "Patched the reply draft."
Move True To iSuccess
End
Else Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Response status code = " iTemp1
Showln "Failed to patch the reply draft."
Move False To iSuccess
End
// Show the response in both cases..
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoJsonResponse sTemp1 To iSuccess
Get ComEmit Of hoJsonResponse To sTemp1
Showln sTemp1
If (iSuccess = False) Begin
Procedure_Return
End
// ---------------------------------------------------------------------
// OK, let's send the reply email...
// To send, POST with an empty request body:
// POST https://graph.microsoft.com/v1.0/me/messages/{id}/send
Get pvComObject of hoResp to vResp
Get ComHttpStr Of hoHttp "POST" "https://graph.microsoft.com/v1.0/me/messages/{$message_id}/send" "" "" "" vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
// A 202 response indicates success.
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 202) Begin
Showln "Sent the email reply."
// If the status code was 202, there is no response body.
End
Else Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Response status code = " iTemp1
Showln "Failed to send the email reply."
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoJsonResponse sTemp1 To iSuccess
Get ComEmit Of hoJsonResponse To sTemp1
Showln sTemp1
End
Showln "Finished."
// -----------------------------------------------------------------------
// A sample successful JSON response for the createReply looks like this:
// {
// "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#message",
// "@odata.type": "#microsoft.graph.message",
// "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADQAwR4\"",
// "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM-lQbAAAAA=",
// "createdDateTime": "2017-06-01T14:31:56Z",
// "lastModifiedDateTime": "2017-06-01T14:31:56Z",
// "changeKey": "CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADQAwR4",
// "categories": [
// ],
// "receivedDateTime": "2017-06-01T14:31:56Z",
// "sentDateTime": "2017-06-01T14:31:56Z",
// "hasAttachments": false,
// "internetMessageId": "<SN1PR20MB0461AF40FC899A8462536F04E6F60@SN1PR20MB0461.namprd20.prod.outlook.com>",
// "subject": "RE: This email contains a PDF attachment",
// "bodyPreview": "________________________________\r\nFrom: Chilkat Software <support@chilkatsoft.com>\r\nSent: Tuesday, May 30, 2017 10:58:56 PM\r\nTo: chilkatsoft@outlook.com\r\nSubject: This email contains a PDF attachment\r\n\r\nThis email contains a PDF attachment\r\n--\r\nBest Regar",
// "importance": "normal",
// "parentFolderId": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA",
// "conversationId": "AQQkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAQAMMaWY-CWmVOqNKt-NiVhkU=",
// "isDeliveryReceiptRequested": false,
// "isReadReceiptRequested": false,
// "isRead": true,
// "isDraft": true,
// "webLink": "https://outlook.live.com/owa/?ItemID=AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5%2BvF7TKKdE6bGCRqXyl2PQAAAgEPAAAA5%2BvF7TKKdE6bGCRqXyl2PQAAAM%2FlQbAAAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
// "inferenceClassification": "focused",
// "body": {
// "contentType": "html",
// "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n<meta content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body bgcolor=\"#FFFFFF\">\r\n<hr tabindex=\"-1\" style=\"display:inline-block; width:98%\"> ... </html>\r\n"
// },
// "sender": {
// "emailAddress": {
// "name": "Matt Smith",
// "address": "chilkatsoft@outlook.com"
// }
// },
// "from": {
// "emailAddress": {
// "name": "Matt Smith",
// "address": "chilkatsoft@outlook.com"
// }
// },
// "toRecipients": [
// {
// "emailAddress": {
// "name": "Chilkat Software",
// "address": "support@chilkatsoft.com"
// }
// }
// ],
// "ccRecipients": [
// ],
// "bccRecipients": [
// ],
// "replyTo": [
// ]
// }
//
// -----------------------------------------------------------------------
// A sample successful PATCH JSON response looks like this:
// {
// "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/messages/$entity",
// "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADQAwSA\"",
// "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM-lQbMAAAA=",
// "createdDateTime": "2017-06-01T15:18:14Z",
// "lastModifiedDateTime": "2017-06-01T15:18:17Z",
// "changeKey": "CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADQAwSA",
// "categories": [
// ],
// "receivedDateTime": "2017-06-01T15:18:15Z",
// "sentDateTime": "2017-06-01T15:18:15Z",
// "hasAttachments": false,
// "internetMessageId": "<SN1PR20MB046138C4A26051200764FE38E6F60@SN1PR20MB0461.namprd20.prod.outlook.com>",
// "subject": "RE: This email has the word Amazon in the subject..",
// "bodyPreview": "This is my response.\r\n\r\n________________________________\r\nFrom: Chilkat Software <support@chilkatsoft.com>\r\nSent: Tuesday, May 30, 2017 11:40:01 PM\r\nTo: Chilkat Software\r\nSubject: This email has the word Amazon in the subject..\r\n\r\nThis email has the word ",
// "importance": "normal",
// "parentFolderId": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA",
// "conversationId": "AQQkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAQAE64PfPP1n1IhU4YasnNN0Q=",
// "isDeliveryReceiptRequested": false,
// "isReadReceiptRequested": false,
// "isRead": true,
// "isDraft": true,
// "webLink": "https://outlook.live.com/owa/?ItemID=AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5%2BvF7TKKdE6bGCRqXyl2PQAAAgEPAAAA5%2BvF7TKKdE6bGCRqXyl2PQAAAM%2FlQbMAAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
// "inferenceClassification": "focused",
// "body": {
// "contentType": "html",
// "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n<meta content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body bgcolor=\"#FFFFFF\">\r\n<p>This is my response.</p>\r\n<hr tabindex=\"-1\" style=\"display:inline-block; width:98%\">\r\n<div id=\"divRplyFwdMsg\" dir=\"ltr\"><font face=\"Calibri, sans-serif\" color=\"#000000\" style=\"font-size:11pt\"><b>From:</b> Chilkat Software <support@chilkatsoft.com><br>\r\n<b>Sent:</b> Tuesday, May 30, 2017 11:40:01 PM<br>\r\n<b>To:</b> Chilkat Software<br>\r\n<b>Subject:</b> This email has the word Amazon in the subject..</font>\r\n<div> </div>\r\n</div>\r\n<div><font face=\"Calibri\">This email has the word Amazon in the subject..</font><br>\r\n<div class=\"moz-signature\">-- <br>\r\nBest Regards,<br>\r\nMatt Smith<br>\r\nChilkat Software, Inc.<br>\r\n<p><a href=\"https://twitter.com/chilkatsoft\">Follow Chilkat on Twitter</a> </p>\r\n</div>\r\n</div>\r\n</body>\r\n</html>\r\n"
// },
// "sender": {
// "emailAddress": {
// "name": "Matt Smith",
// "address": "chilkatsoft@outlook.com"
// }
// },
// "from": {
// "emailAddress": {
// "name": "Matt Smith",
// "address": "chilkatsoft@outlook.com"
// }
// },
// "toRecipients": [
// {
// "emailAddress": {
// "name": "Chilkat Software",
// "address": "support@chilkatsoft.com"
// }
// }
// ],
// "ccRecipients": [
// ],
// "bccRecipients": [
// ],
// "replyTo": [
// ]
// }
End_Procedure