Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_HtFolderMap
oleobject loo_SbMap
string ls_FolderId
oleobject loo_Json
oleobject loo_SbResponse
string ls_ExistingMsgId
oleobject loo_JsonRequestBody
oleobject loo_Resp
oleobject loo_JsonResponse
string ls_ReplyMsgId
oleobject loo_JsonPatch
oleobject loo_SbHtml
integer li_NumReplaced
integer li_NumToRecipients
integer i
integer li_NumCcRecipients

li_Success = 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Use your previously obtained access token here:
loo_Http.AuthToken = "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 
loo_HtFolderMap = create oleobject
li_rc = loo_HtFolderMap.ConnectToNewObject("Chilkat.Hashtable")

loo_SbMap = create oleobject
li_rc = loo_SbMap.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbMap.LoadFile("qa_data/outlook/folderMap.xml","utf-8")
loo_HtFolderMap.AddFromXmlSb(loo_SbMap)

//  Get the ID for the "/Inbox" folder:
ls_FolderId = loo_HtFolderMap.LookupStr("/Inbox")
if loo_HtFolderMap.LastMethodSuccess <> 1 then
    Write-Debug "Folder ID not found"
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    return
end if

li_Success = 1
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.EmitCompact = 0

//  Search for unread emails in this folder from support@chilkatsoft.com 
loo_Http.SetUrlVar("folder_id",ls_FolderId)
loo_Http.SetUrlVar("select","id,subject")
loo_Http.SetUrlVar("filter","(isRead eq false) and (from/emailAddress/address eq 'support@chilkatsoft.com')")

loo_SbResponse = create oleobject
li_rc = loo_SbResponse.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    return
end if

loo_Json.LoadSb(loo_SbResponse)
//  Show the results..
Write-Debug loo_Json.Emit()

if loo_Json.SizeOfArray("value") = 0 then
    Write-Debug "Empty result set."
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    return
end if

//  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.

ls_ExistingMsgId = loo_Json.StringOf("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"
//  	}

loo_JsonRequestBody = create oleobject
li_rc = loo_JsonRequestBody.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonRequestBody.UpdateString("comment","This is a comment")

loo_Http.SetUrlVar("message_id",ls_ExistingMsgId)

//  Create the reply in the Drafts folder:
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpJson("POST","https://graph.microsoft.com/v1.0/me/messages/{$message_id}/createReply",loo_JsonRequestBody,"application/json",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    destroy loo_JsonRequestBody
    destroy loo_Resp
    return
end if

//  A 201 response indicates success.
if loo_Resp.StatusCode = 201 then
    Write-Debug "Created reply draft."
    li_Success = 1
else
    Write-Debug "Response status code = " + string(loo_Resp.StatusCode)
    Write-Debug "Failed to create reply draft."
    li_Success = 0
end if

//  Show the response in both cases..
loo_JsonResponse = create oleobject
li_rc = loo_JsonResponse.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonResponse.EmitCompact = 0
loo_JsonResponse.Load(loo_Resp.BodyStr)
Write-Debug loo_JsonResponse.Emit()
if li_Success = 0 then
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    destroy loo_JsonRequestBody
    destroy loo_Resp
    destroy loo_JsonResponse
    return
end if

//  ----------------------------------------------
//  Get the message id of the newly created reply.
ls_ReplyMsgId = loo_JsonResponse.StringOf("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"
//  	}

loo_JsonPatch = create oleobject
li_rc = loo_JsonPatch.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonPatch.UpdateString("body.contentType","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.
loo_SbHtml = create oleobject
li_rc = loo_SbHtml.ConnectToNewObject("Chilkat.StringBuilder")

loo_JsonResponse.StringOfSb("body.content",loo_SbHtml)
//  Insert the response HTML in the reply HTML body.
li_NumReplaced = loo_SbHtml.ReplaceBetween("<body bgcolor=~"#FFFFFF~">","<hr ","~r~n","<p>This is my response.</p>")
if li_NumReplaced <> 1 then
    Write-Debug "Something is amiss!"
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    destroy loo_JsonRequestBody
    destroy loo_Resp
    destroy loo_JsonResponse
    destroy loo_JsonPatch
    destroy loo_SbHtml
    return
end if

loo_JsonPatch.UpdateString("body.content",loo_SbHtml.GetAsString())

//  Add additional CC, BCC, or TO recipients like this:
li_NumToRecipients = loo_JsonResponse.SizeOfArray("toRecipients")
i = 0
//  Copy existing TO recipients.
do while i < li_NumToRecipients
    loo_JsonPatch.I = i
    loo_JsonResponse.I = i
    loo_JsonPatch.UpdateString("toRecipients[i].emailAddress.name",loo_JsonResponse.StringOf("toRecipients[i].emailAddress.name"))
    loo_JsonPatch.UpdateString("toRecipients[i].emailAddress.address",loo_JsonResponse.StringOf("toRecipients[i].emailAddress.address"))
    i = i + 1
loop
//  Add an additional TO recipient.
loo_JsonPatch.I = li_NumToRecipients
loo_JsonPatch.UpdateString("toRecipients[i].emailAddress.name","Chilkat")
loo_JsonPatch.UpdateString("toRecipients[i].emailAddress.address","admin@chilkat.io")

li_NumCcRecipients = loo_JsonResponse.SizeOfArray("ccRecipients")
i = 0
//  Copy existing CC recipients.
do while i < li_NumCcRecipients
    loo_JsonPatch.I = i
    loo_JsonResponse.I = i
    loo_JsonPatch.UpdateString("ccRecipients[i].emailAddress.name",loo_JsonResponse.StringOf("ccRecipients[i].emailAddress.name"))
    loo_JsonPatch.UpdateString("ccRecipients[i].emailAddress.address",loo_JsonResponse.StringOf("ccRecipients[i].emailAddress.address"))
    i = i + 1
loop
//  Add an additional CC recipient.
loo_JsonPatch.I = li_NumCcRecipients
loo_JsonPatch.UpdateString("ccRecipients[i].emailAddress.name","Chilkat GMail")
loo_JsonPatch.UpdateString("ccRecipients[i].emailAddress.address","chilkat.support@gmail.com")

loo_JsonPatch.EmitCompact = 0
Write-Debug loo_JsonPatch.Emit()

loo_Http.SetUrlVar("message_id",ls_ReplyMsgId)
li_Success = loo_Http.HttpJson("PATCH","https://graph.microsoft.com/v1.0/me/messages/{$message_id}",loo_JsonPatch,"application/json",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    destroy loo_JsonRequestBody
    destroy loo_Resp
    destroy loo_JsonResponse
    destroy loo_JsonPatch
    destroy loo_SbHtml
    return
end if

//  A 200 response indicates success.
if loo_Resp.StatusCode = 200 then
    Write-Debug "Patched the reply draft."
    li_Success = 1
else
    Write-Debug "Response status code = " + string(loo_Resp.StatusCode)
    Write-Debug "Failed to patch the reply draft."
    li_Success = 0
end if

//  Show the response in both cases..
loo_JsonResponse.Load(loo_Resp.BodyStr)
Write-Debug loo_JsonResponse.Emit()
if li_Success = 0 then
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    destroy loo_JsonRequestBody
    destroy loo_Resp
    destroy loo_JsonResponse
    destroy loo_JsonPatch
    destroy loo_SbHtml
    return
end if

//  ---------------------------------------------------------------------
//  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
li_Success = loo_Http.HttpStr("POST","https://graph.microsoft.com/v1.0/me/messages/{$message_id}/send","","","",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbResponse
    destroy loo_JsonRequestBody
    destroy loo_Resp
    destroy loo_JsonResponse
    destroy loo_JsonPatch
    destroy loo_SbHtml
    return
end if

//  A 202 response indicates success.
if loo_Resp.StatusCode = 202 then
    Write-Debug "Sent the email reply."
    //  If the status code was 202, there is no response body.
else
    Write-Debug "Response status code = " + string(loo_Resp.StatusCode)
    Write-Debug "Failed to send the email reply."
    loo_JsonResponse.Load(loo_Resp.BodyStr)
    Write-Debug loo_JsonResponse.Emit()
end if

Write-Debug "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 &lt;support@chilkatsoft.com&gt;<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>&nbsp;</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": [
//  	  ]
//  	}


destroy loo_Http
destroy loo_HtFolderMap
destroy loo_SbMap
destroy loo_Json
destroy loo_SbResponse
destroy loo_JsonRequestBody
destroy loo_Resp
destroy loo_JsonResponse
destroy loo_JsonPatch
destroy loo_SbHtml