Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Outlook -- Create Reply Email, Update, and SendCreates a reply email in the Drafts folder, updates the reply with information, and then sends the reply. Note: This example requires Chilkat v9.5.0.68 or greater. This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
#include <C_CkHttpW.h> #include <C_CkHashtableW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkHttpW http; HCkHashtableW htFolderMap; HCkStringBuilderW sbMap; const wchar_t *folderId; BOOL success; HCkJsonObjectW json; HCkStringBuilderW sbResponse; const wchar_t *existingMsgId; HCkJsonObjectW jsonRequestBody; HCkHttpResponseW resp; HCkJsonObjectW jsonResponse; const wchar_t *replyMsgId; HCkJsonObjectW jsonPatch; HCkStringBuilderW sbHtml; int numReplaced; int numToRecipients; int i; int numCcRecipients; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // Use your previously obtained access token here: CkHttpW_putAuthToken(http,L"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 htFolderMap = CkHashtableW_Create(); sbMap = CkStringBuilderW_Create(); CkStringBuilderW_LoadFile(sbMap,L"qa_data/outlook/folderMap.xml",L"utf-8"); CkHashtableW_AddFromXmlSb(htFolderMap,sbMap); // Get the ID for the "/Inbox" folder: folderId = CkHashtableW_lookupStr(htFolderMap,L"/Inbox"); if (CkHashtableW_getLastMethodSuccess(htFolderMap) != TRUE) { wprintf(L"Folder ID not found\n"); CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); return; } success = TRUE; json = CkJsonObjectW_Create(); CkJsonObjectW_putEmitCompact(json,FALSE); // Search for unread emails in this folder from support@chilkatsoft.com CkHttpW_SetUrlVar(http,L"folder_id",folderId); CkHttpW_SetUrlVar(http,L"select",L"id,subject"); CkHttpW_SetUrlVar(http,L"filter",L"(isRead eq false) and (from/emailAddress/address eq 'support@chilkatsoft.com')"); sbResponse = CkStringBuilderW_Create(); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if (success != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); return; } CkJsonObjectW_LoadSb(json,sbResponse); // Show the results.. wprintf(L"%s\n",CkJsonObjectW_emit(json)); if (CkJsonObjectW_SizeOfArray(json,L"value") == 0) { wprintf(L"Empty result set.\n"); CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); return; } // 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. existingMsgId = CkJsonObjectW_stringOf(json,L"value[0].id"); // 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" // } jsonRequestBody = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(jsonRequestBody,L"comment",L"This is a comment"); CkHttpW_SetUrlVar(http,L"message_id",existingMsgId); // Create the reply in the Drafts folder: resp = CkHttpW_PostJson2(http,L"https://graph.microsoft.com/v1.0/me/messages/{$message_id}/createReply",L"application/json",CkJsonObjectW_emit(jsonRequestBody)); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(jsonRequestBody); return; } // A 201 response indicates success. if (CkHttpResponseW_getStatusCode(resp) == 201) { wprintf(L"Created reply draft.\n"); success = TRUE; } else { wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp)); wprintf(L"Failed to create reply draft.\n"); success = FALSE; } // Show the response in both cases.. jsonResponse = CkJsonObjectW_Create(); CkJsonObjectW_putEmitCompact(jsonResponse,FALSE); CkJsonObjectW_Load(jsonResponse,CkHttpResponseW_bodyStr(resp)); wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); CkHttpResponseW_Dispose(resp); if (success == FALSE) { CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(jsonRequestBody); CkJsonObjectW_Dispose(jsonResponse); return; } // ---------------------------------------------- // Get the message id of the newly created reply. replyMsgId = CkJsonObjectW_stringOf(jsonResponse,L"id"); // 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" // } jsonPatch = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(jsonPatch,L"body.contentType",L"html"); // 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. sbHtml = CkStringBuilderW_Create(); CkJsonObjectW_StringOfSb(jsonResponse,L"body.content",sbHtml); // Insert the response HTML in the reply HTML body. numReplaced = CkStringBuilderW_ReplaceBetween(sbHtml,L"<body bgcolor=\"#FFFFFF\">",L"<hr ",L"\r\n",L"<p>This is my response.</p>"); if (numReplaced != 1) { wprintf(L"Something is amiss!\n"); CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(jsonRequestBody); CkJsonObjectW_Dispose(jsonResponse); CkJsonObjectW_Dispose(jsonPatch); CkStringBuilderW_Dispose(sbHtml); return; } CkJsonObjectW_UpdateString(jsonPatch,L"body.content",CkStringBuilderW_getAsString(sbHtml)); // Add additional CC, BCC, or TO recipients like this: numToRecipients = CkJsonObjectW_SizeOfArray(jsonResponse,L"toRecipients"); i = 0; // Copy existing TO recipients. while (i < numToRecipients) { CkJsonObjectW_putI(jsonPatch,i); CkJsonObjectW_putI(jsonResponse,i); CkJsonObjectW_UpdateString(jsonPatch,L"toRecipients[i].emailAddress.name",CkJsonObjectW_stringOf(jsonResponse,L"toRecipients[i].emailAddress.name")); CkJsonObjectW_UpdateString(jsonPatch,L"toRecipients[i].emailAddress.address",CkJsonObjectW_stringOf(jsonResponse,L"toRecipients[i].emailAddress.address")); i = i + 1; } // Add an additional TO recipient. CkJsonObjectW_putI(jsonPatch,numToRecipients); CkJsonObjectW_UpdateString(jsonPatch,L"toRecipients[i].emailAddress.name",L"Chilkat"); CkJsonObjectW_UpdateString(jsonPatch,L"toRecipients[i].emailAddress.address",L"admin@chilkat.io"); numCcRecipients = CkJsonObjectW_SizeOfArray(jsonResponse,L"ccRecipients"); i = 0; // Copy existing CC recipients. while (i < numCcRecipients) { CkJsonObjectW_putI(jsonPatch,i); CkJsonObjectW_putI(jsonResponse,i); CkJsonObjectW_UpdateString(jsonPatch,L"ccRecipients[i].emailAddress.name",CkJsonObjectW_stringOf(jsonResponse,L"ccRecipients[i].emailAddress.name")); CkJsonObjectW_UpdateString(jsonPatch,L"ccRecipients[i].emailAddress.address",CkJsonObjectW_stringOf(jsonResponse,L"ccRecipients[i].emailAddress.address")); i = i + 1; } // Add an additional CC recipient. CkJsonObjectW_putI(jsonPatch,numCcRecipients); CkJsonObjectW_UpdateString(jsonPatch,L"ccRecipients[i].emailAddress.name",L"Chilkat GMail"); CkJsonObjectW_UpdateString(jsonPatch,L"ccRecipients[i].emailAddress.address",L"chilkat.support@gmail.com"); CkJsonObjectW_putEmitCompact(jsonPatch,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(jsonPatch)); CkHttpW_SetUrlVar(http,L"message_id",replyMsgId); resp = CkHttpW_PText(http,L"PATCH",L"https://graph.microsoft.com/v1.0/me/messages/{$message_id}",CkJsonObjectW_emit(jsonPatch),L"utf-8",L"application/json",FALSE,FALSE); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(jsonRequestBody); CkJsonObjectW_Dispose(jsonResponse); CkJsonObjectW_Dispose(jsonPatch); CkStringBuilderW_Dispose(sbHtml); return; } // A 200 response indicates success. if (CkHttpResponseW_getStatusCode(resp) == 200) { wprintf(L"Patched the reply draft.\n"); success = TRUE; } else { wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp)); wprintf(L"Failed to patch the reply draft.\n"); success = FALSE; } // Show the response in both cases.. CkJsonObjectW_Load(jsonResponse,CkHttpResponseW_bodyStr(resp)); wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); CkHttpResponseW_Dispose(resp); if (success == FALSE) { CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(jsonRequestBody); CkJsonObjectW_Dispose(jsonResponse); CkJsonObjectW_Dispose(jsonPatch); CkStringBuilderW_Dispose(sbHtml); return; } // --------------------------------------------------------------------- // 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 resp = CkHttpW_PText(http,L"POST",L"https://graph.microsoft.com/v1.0/me/messages/{$message_id}/send",L"",L"",L"",FALSE,FALSE); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(jsonRequestBody); CkJsonObjectW_Dispose(jsonResponse); CkJsonObjectW_Dispose(jsonPatch); CkStringBuilderW_Dispose(sbHtml); return; } // A 202 response indicates success. if (CkHttpResponseW_getStatusCode(resp) == 202) { wprintf(L"Sent the email reply.\n"); // If the status code was 202, there is no response body. } else { wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp)); wprintf(L"Failed to send the email reply.\n"); CkJsonObjectW_Load(jsonResponse,CkHttpResponseW_bodyStr(resp)); wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); } CkHttpResponseW_Dispose(resp); wprintf(L"Finished.\n"); // ----------------------------------------------------------------------- // 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": [ // ] // } CkHttpW_Dispose(http); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(jsonRequestBody); CkJsonObjectW_Dispose(jsonResponse); CkJsonObjectW_Dispose(jsonPatch); CkStringBuilderW_Dispose(sbHtml); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.