Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
-- Use this online tool to generate code from sample JSON: Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "data": [
-- {
-- "from": {
-- "user_name": "Patricia Boyle",
-- "email": "p.boyle@abc.com"
-- },
-- "to": [
-- {
-- "user_name": "user1",
-- "email": "user1@gmail.com"
-- }
-- ],
-- "cc": [
-- {
-- "user_name": "user2",
-- "email": "user2email@zoho.com"
-- }
-- ],
-- "bcc": [
-- {
-- "user_name": "user3",
-- "email": "user3email@zoho.com"
-- }
-- ],
-- "reply_to": {
-- "user_name": "user4",
-- "email": "user4email@zoho.com"
-- },
-- "org_email": false,
-- "in_reply_to": "2cceafa194d037b63f2000181dd8186486f1eb0360aee76d802b6d376dea97e7",
-- "scheduled_time": "2020-06-12T12:12:12+05:30",
-- "subject": "Testing Send Mail API",
-- "content": "<h3><span style=\"background-color: rgb(254, 255, 102)\">Mail is of rich text format</span></h3><h3><span style=\"background-color: rgb(254, 255, 102)\"><img src=\"https://www.zohoapis.com/crm/viewInLineImage?fileContent=2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2\"></span></h3><h3><span style=\"background-color: rgb(254, 255, 102)\">REGARDS,</span></h3><div><span style=\"background-color: rgb(254, 255, 102)\">AZ</span></div><div><span style=\"background-color: rgb(254, 255, 102)\">ADMIN</span></div> <div></div>",
-- "mail_format": "html",
-- "attachments": [
-- {
-- "id": "2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2"
-- }
-- ]
-- }
-- ]
-- }
DECLARE @json int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].from.user_name', 'Patricia Boyle'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].from.email', 'p.boyle@abc.com'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].to[0].user_name', 'user1'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].to[0].email', 'user1@gmail.com'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].cc[0].user_name', 'user2'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].cc[0].email', 'user2email@zoho.com'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].bcc[0].user_name', 'user3'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].bcc[0].email', 'user3email@zoho.com'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].reply_to.user_name', 'user4'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].reply_to.email', 'user4email@zoho.com'
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'data[0].org_email', 0
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].in_reply_to', '2cceafa194d037b63f2000181dd8186486f1eb0360aee76d802b6d376dea97e7'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].scheduled_time', '2020-06-12T12:12:12+05:30'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].subject', 'Testing Send Mail API'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].content', '<h3><span style="background-color: rgb(254, 255, 102)">Mail is of rich text format</span></h3><h3><span style="background-color: rgb(254, 255, 102)"><img src="https://www.zohoapis.com/crm/viewInLineImage?fileContent=2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2"></span></h3><h3><span style="background-color: rgb(254, 255, 102)">REGARDS,</span></h3><div><span style="background-color: rgb(254, 255, 102)">AZ</span></div><div><span style="background-color: rgb(254, 255, 102)">ADMIN</span></div> <div></div>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].mail_format', 'html'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].attachments[0].id', '2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2'
-- Adds the "Authorization: Bearer <access_token>" header.
EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
DECLARE @resp int
EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/crm/v2.1/Leads/{{record_id}}/actions/send_mail', 'application/json', @json
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
RETURN
END
DECLARE @sbResponseBody int
-- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
EXEC @hr = sp_OADestroy @resp
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "data": [
-- {
-- "code": "SUCCESS",
-- "details": {
-- "message_id": "9943645bd4411e51145e0515bd77e38058ef4386573e33fc3c5a988ba3a2dead"
-- },
-- "message": "Your mail has been scheduled successfully.",
-- "status": "success"
-- }
-- ]
-- }
-- Sample code for parsing the JSON response...
-- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
DECLARE @code nvarchar(4000)
DECLARE @Message_id nvarchar(4000)
DECLARE @message nvarchar(4000)
DECLARE @status nvarchar(4000)
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'data'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @code OUT, 'data[i].code'
EXEC sp_OAMethod @jResp, 'StringOf', @Message_id OUT, 'data[i].details.message_id'
EXEC sp_OAMethod @jResp, 'StringOf', @message OUT, 'data[i].message'
EXEC sp_OAMethod @jResp, 'StringOf', @status OUT, 'data[i].status'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-d '{
"data": [
{
"from": {
"user_name": "Patricia Boyle",
"email": "p.boyle@abc.com"
},
"to": [
{
"user_name": "user1",
"email": "user1@gmail.com"
}
],
"cc": [
{
"user_name": "user2",
"email": "user2email@zoho.com"
}
],
"bcc": [
{
"user_name": "user3",
"email": "user3email@zoho.com"
}
],
"reply_to": {
"user_name": "user4",
"email": "user4email@zoho.com"
},
"org_email": false,
"in_reply_to": "2cceafa194d037b63f2000181dd8186486f1eb0360aee76d802b6d376dea97e7",
"scheduled_time": "2020-06-12T12:12:12+05:30",
"subject": "Testing Send Mail API",
"content": "<h3><span style=\"background-color: rgb(254, 255, 102)\">Mail is of rich text format</span></h3><h3><span style=\"background-color: rgb(254, 255, 102)\"><img src=\"https://www.zohoapis.com/crm/viewInLineImage?fileContent=2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2\"></span></h3><h3><span style=\"background-color: rgb(254, 255, 102)\">REGARDS,</span></h3><div><span style=\"background-color: rgb(254, 255, 102)\">AZ</span></div><div><span style=\"background-color: rgb(254, 255, 102)\">ADMIN</span></div> <div></div>",
"mail_format": "html",
"attachments": [
{
"id": "2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2"
}
]
}
]
}'
https://domain.com/crm/v2.1/Leads/{{record_id}}/actions/send_mail
Postman Collection Item JSON
{
"name": "Send Mail",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access-token}}",
"type": "string"
}
]
},
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"p.boyle@abc.com\"\n },\n \"to\": [\n {\n \"user_name\": \"user1\",\n \"email\": \"user1@gmail.com\"\n }\n ],\n \"cc\": [\n {\n \"user_name\": \"user2\",\n \"email\": \"user2email@zoho.com\"\n }\n ],\n \"bcc\": [\n {\n \"user_name\": \"user3\",\n \"email\": \"user3email@zoho.com\"\n }\n ],\n \"reply_to\": {\n \"user_name\": \"user4\",\n \"email\": \"user4email@zoho.com\"\n },\n \"org_email\": false,\n \"in_reply_to\": \"2cceafa194d037b63f2000181dd8186486f1eb0360aee76d802b6d376dea97e7\",\n \"scheduled_time\": \"2020-06-12T12:12:12+05:30\",\n \"subject\": \"Testing Send Mail API\",\n \"content\": \"<h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">Mail is of rich text format</span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\"><img src=\\\"https://www.zohoapis.com/crm/viewInLineImage?fileContent=2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2\\\"></span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">REGARDS,</span></h3><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">AZ</span></div><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">ADMIN</span></div> <div></div>\",\n \"mail_format\": \"html\",\n \"attachments\": [\n {\n \"id\": \"2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2\"\n }\n ]\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Leads/{{record_id}}/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Leads",
"{{record_id}}",
"actions",
"send_mail"
]
}
},
"response": [
{
"name": "SUCCESS RESPONSE",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"patricia.b@zylker.com\"\n },\n \"to\": [\n {\n \"user_name\": \"User 2\",\n \"email\": \"user2@gmail.com\"\n }\n ],\n \n \"reply_to\": {\n \"user_name\": \"User 3\",\n \"email\": \"user3@gmail.com\"\n },\n \"org_email\": false,\n \"in_reply_to\": \"2cceafa194d037b63f2000181dd8186486f1eb0360aee76d802b6d376dea97e7\",\n \"scheduled_time\": \"2021-06-12T12:12:12+05:30\",\n \"subject\": \"Testing Send Mail API\",\n \"content\": \"<h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">Mail is of rich text format</span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\"><img src=\\\"https://www.zohoapis.com/crm/viewInLineImage?fileContent=2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2\\\"></span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">REGARDS,</span></h3><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">AZ</span></div><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">ADMIN</span></div> <div></div>\",\n \"mail_format\": \"html\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Leads/4150868000004479013/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Leads",
"4150868000004479013",
"actions",
"send_mail"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:38:49 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "195"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Referrer-Policy",
"value": "strict-origin"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-ACCESSTOKEN-RESET",
"value": "2021-05-10T19:05:30+05:30"
},
{
"key": "clientVersion",
"value": "4019232"
},
{
"key": "clientsubVersion",
"value": "19804a82a902c9fa618d2b359b97e26a"
},
{
"key": "Content-Disposition",
"value": "attachment; filename=response.json"
},
{
"key": "Content-Language",
"value": "en-US"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=63072000"
}
],
"cookie": [
],
"body": "{\n \"data\": [\n {\n \"code\": \"SUCCESS\",\n \"details\": {\n \"message_id\": \"9943645bd4411e51145e0515bd77e38058ef4386573e33fc3c5a988ba3a2dead\"\n },\n \"message\": \"Your mail has been scheduled successfully.\",\n \"status\": \"success\"\n }\n ]\n}"
},
{
"name": "INVALID_REQUEST_METHOD",
"originalRequest": {
"method": "COPY",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"patricia.b@zylker.com\"\n },\n \"to\": [\n {\n \"user_name\": \"User 2\",\n \"email\": \"user2@gmail.com\"\n }\n ],\n \n \"reply_to\": {\n \"user_name\": \"User 3\",\n \"email\": \"user3@gmail.com\"\n },\n \"org_email\": false,\n \"in_reply_to\": \"2cceafa194d037b63f2000181dd8186486f1eb0360aee76d802b6d376dea97e7\",\n \"scheduled_time\": \"2021-06-12T12:12:12+05:30\",\n \"subject\": \"Testing Send Mail API\",\n \"content\": \"<h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">Mail is of rich text format</span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\"><img src=\\\"https://www.zohoapis.com/crm/viewInLineImage?fileContent=2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2\\\"></span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">REGARDS,</span></h3><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">AZ</span></div><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">ADMIN</span></div> <div></div>\",\n \"mail_format\": \"html\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Leads/4150868000004479013/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Leads",
"4150868000004479013",
"actions",
"send_mail"
]
}
},
"status": "Bad Request",
"code": 400,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:39:53 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "124"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "X-Frame-Options",
"value": "deny"
},
{
"key": "X-Download-Options",
"value": "noopen"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
}
],
"cookie": [
],
"body": "{\n \"code\": \"INVALID_REQUEST_METHOD\",\n \"details\": {},\n \"message\": \"The http request method type is not a valid one\",\n \"status\": \"error\"\n}"
},
{
"name": "AUTHENTICATION_FAILURE",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"patricia.b@zylker.com\"\n },\n \"to\": [\n {\n \"user_name\": \"User 2\",\n \"email\": \"user2@gmail.com\"\n }\n ],\n \n \"reply_to\": {\n \"user_name\": \"User 3\",\n \"email\": \"user3@gmail.com\"\n },\n \"org_email\": false,\n \"in_reply_to\": \"2cceafa194d037b63f2000181dd8186486f1eb0360aee76d802b6d376dea97e7\",\n \"scheduled_time\": \"2021-06-12T12:12:12+05:30\",\n \"subject\": \"Testing Send Mail API\",\n \"content\": \"<h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">Mail is of rich text format</span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\"><img src=\\\"https://www.zohoapis.com/crm/viewInLineImage?fileContent=2cceafa194d037b63f2000181dd818646fd5e5167a274098b625c35654a20ed2\\\"></span></h3><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">REGARDS,</span></h3><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">AZ</span></div><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">ADMIN</span></div> <div></div>\",\n \"mail_format\": \"html\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Leads/4150868000004479013/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Leads",
"4150868000004479013",
"actions",
"send_mail"
]
}
},
"status": "Unauthorized",
"code": 401,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:41:57 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "98"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Referrer-Policy",
"value": "strict-origin"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-Download-Options",
"value": "noopen"
}
],
"cookie": [
],
"body": "{\n \"code\": \"AUTHENTICATION_FAILURE\",\n \"details\": {},\n \"message\": \"Authentication failed\",\n \"status\": \"error\"\n}"
}
]
}