Sample code for 30+ languages & platforms
Delphi ActiveX

MedTunnel: Send Message Text Only

See more MedTunnel Examples

Demonstrates the MedTunnel SendMessage method to send a message to one or more recipients.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
http: TChilkatHttp;
req: TChilkatHttpRequest;
resp: TChilkatHttpResponse;
sbResponseBody: TChilkatStringBuilder;
jResp: TChilkatJsonObject;
respStatusCode: Integer;
ReturnCode: Integer;
ReturnCodeText: WideString;
Data: WideString;
jsonData: TChilkatJsonObject;
UserName: WideString;
AccountId: Integer;
AccountName: WideString;
AccountTitle: WideString;
FirstName: WideString;
LastName: WideString;
LastSentOn: WideString;
SendCount: Integer;
IsFavorite: Integer;
Id: Integer;
FromUserId: Integer;
FromMailBoxId: Integer;
FromUserType: Integer;
FromUserName: WideString;
FromUserFullName: WideString;
FromUserAccountName: WideString;
FromUserAccountTitle: WideString;
ToUserId: Integer;
ToUserType: Integer;
ToUserMailboxId: WideString;
ToUserName: WideString;
ToUserFullName: WideString;
EmailAddress: WideString;
Password: WideString;
Subject: WideString;
PatientMedTunnelId: WideString;
Body: WideString;
DateReceived: WideString;
DisplayDateReceived: WideString;
ViewCount: Integer;
ViewedOn: WideString;
AttachmentCount: Integer;
Status: Integer;
ParentMessageId: Integer;
DistributionListId: Integer;
DistributionListName: WideString;
BodyHistory: WideString;
ReadReceiptCallbackUrl: WideString;
SendGlobalNotifications: Integer;
i: Integer;
count_i: Integer;

begin
success := 0;

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

http := TChilkatHttp.Create(Self);

// Implements the following CURL command:

// curl https://server.medtunnel.com/MedTunnelMsg/api/Message/SendMessage -X POST -k 
//         -F "ApplicationId=yourApplicationId" -F "LocationId=yourLocationId" 
//         -F "MedTunnelId=yourMedTunnelId" -F "MedTunnelPassword=yourMedTunnelPassword" 
//         -F "To=recipientsMedTunnelId" 
//         -F "Body=Test of SendMessage"

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

req := TChilkatHttpRequest.Create(Self);
req.HttpVerb := 'POST';
req.Path := '/MedTunnelMsg/api/Message/SendMessage';
req.ContentType := 'multipart/form-data';
req.AddParam('ApplicationId','yourApplicationId');
req.AddParam('LocationId','yourLocationId');
req.AddParam('MedTunnelId','yourMedTunnelId');
req.AddParam('MedTunnelPassword','yourMedTunnelPassword');
req.AddParam('To','recipientsMedTunnelId');
req.AddParam('Body','Test');

resp := TChilkatHttpResponse.Create(Self);
success := http.HttpSReq('server.medtunnel.com',443,1,req.ControlInterface,resp.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

sbResponseBody := TChilkatStringBuilder.Create(Self);
resp.GetBodySb(sbResponseBody.ControlInterface);
jResp := TChilkatJsonObject.Create(Self);
jResp.LoadSb(sbResponseBody.ControlInterface);
jResp.EmitCompact := 0;

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(jResp.Emit());

respStatusCode := resp.StatusCode;
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(resp.Header);
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "ReturnCode": 1,
//   "ReturnCodeText": "Success",
//   "Data": "{\"Id\":989432,\"FromUserId\":36990,\"FromMailBoxId\":36965, ... \"SendGlobalNotifications\":false}"
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

ReturnCode := jResp.IntOf('ReturnCode');
ReturnCodeText := jResp.StringOf('ReturnCodeText');
Data := jResp.StringOf('Data');

// Load the Data into another JSON object and parse..
jsonData := TChilkatJsonObject.Create(Self);
jsonData.Load(Data);
jsonData.EmitCompact := 0;
Memo1.Lines.Add(jsonData.Emit());

// {
//   "Id": 989436,
//   "FromUserId": 36990,
//   "FromMailBoxId": 36965,
//   "FromUserType": 0,
//   "FromUserName": "...",
//   "FromUserFullName": "...",
//   "FromUserAccountName": "...",
//   "FromUserAccountTitle": "...",
//   "ToUserId": 36990,
//   "ToUserType": 1,
//   "ToUserMailboxId": "36965",
//   "ToUserName": "...",
//   "ToUserFullName": "...",
//   "EmailAddress": "",
//   "Password": "",
//   "Subject": "",
//   "PatientMedTunnelId": "",
//   "Body": "Test",
//   "DateReceived": "4/29/2021 2:29:18 PM",
//   "DisplayDateReceived": "04/29/2021  2:29 PM",
//   "ViewCount": 0,
//   "ViewedOn": "",
//   "AttachmentCount": 0,
//   "AttachmentNames": [
//   ],
//   "AllRecipients": [
//     {
//       "Id": 989436,
//       "UserName": "...",
//       "AccountId": 0,
//       "AccountName": "...",
//       "AccountTitle": "",
//       "FirstName": "...",
//       "LastName": "...",
//       "EmailAddress": "",
//       "LastSentOn": "",
//       "SendCount": 0,
//       "IsFavorite": false
//     }
//   ],
//   "Status": 1,
//   "ParentMessageId": 989436,
//   "DistributionListId": 0,
//   "DistributionListName": "",
//   "BodyHistory": "",
//   "ReadReceiptCallbackUrl": null,
//   "SendGlobalNotifications": false
// }
// 

Id := jsonData.IntOf('Id');
FromUserId := jsonData.IntOf('FromUserId');
FromMailBoxId := jsonData.IntOf('FromMailBoxId');
FromUserType := jsonData.IntOf('FromUserType');
FromUserName := jsonData.StringOf('FromUserName');
FromUserFullName := jsonData.StringOf('FromUserFullName');
FromUserAccountName := jsonData.StringOf('FromUserAccountName');
FromUserAccountTitle := jsonData.StringOf('FromUserAccountTitle');
ToUserId := jsonData.IntOf('ToUserId');
ToUserType := jsonData.IntOf('ToUserType');
ToUserMailboxId := jsonData.StringOf('ToUserMailboxId');
ToUserName := jsonData.StringOf('ToUserName');
ToUserFullName := jsonData.StringOf('ToUserFullName');
EmailAddress := jsonData.StringOf('EmailAddress');
Password := jsonData.StringOf('Password');
Subject := jsonData.StringOf('Subject');
PatientMedTunnelId := jsonData.StringOf('PatientMedTunnelId');
Body := jsonData.StringOf('Body');
DateReceived := jsonData.StringOf('DateReceived');
DisplayDateReceived := jsonData.StringOf('DisplayDateReceived');
ViewCount := jsonData.IntOf('ViewCount');
ViewedOn := jsonData.StringOf('ViewedOn');
AttachmentCount := jsonData.IntOf('AttachmentCount');
Status := jsonData.IntOf('Status');
ParentMessageId := jsonData.IntOf('ParentMessageId');
DistributionListId := jsonData.IntOf('DistributionListId');
DistributionListName := jsonData.StringOf('DistributionListName');
BodyHistory := jsonData.StringOf('BodyHistory');
ReadReceiptCallbackUrl := jsonData.StringOf('ReadReceiptCallbackUrl');
SendGlobalNotifications := jsonData.BoolOf('SendGlobalNotifications');
i := 0;
count_i := jsonData.SizeOfArray('AttachmentNames');
while i < count_i do
  begin
    jsonData.I := i;
    i := i + 1;
  end;

i := 0;
count_i := jsonData.SizeOfArray('AllRecipients');
while i < count_i do
  begin
    jsonData.I := i;
    Id := jsonData.IntOf('AllRecipients[i].Id');
    UserName := jsonData.StringOf('AllRecipients[i].UserName');
    AccountId := jsonData.IntOf('AllRecipients[i].AccountId');
    AccountName := jsonData.StringOf('AllRecipients[i].AccountName');
    AccountTitle := jsonData.StringOf('AllRecipients[i].AccountTitle');
    FirstName := jsonData.StringOf('AllRecipients[i].FirstName');
    LastName := jsonData.StringOf('AllRecipients[i].LastName');
    EmailAddress := jsonData.StringOf('AllRecipients[i].EmailAddress');
    LastSentOn := jsonData.StringOf('AllRecipients[i].LastSentOn');
    SendCount := jsonData.IntOf('AllRecipients[i].SendCount');
    IsFavorite := jsonData.BoolOf('AllRecipients[i].IsFavorite');
    i := i + 1;
  end;
end;