Sample code for 30+ languages & platforms
Delphi DLL

MedTunnel: Send Message with File Attachment

See more MedTunnel Examples

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

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, HttpRequest, HttpResponse, StringBuilder, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
req: HCkHttpRequest;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
ReturnCode: Integer;
ReturnCodeText: PWideChar;
Data: PWideChar;
jsonData: HCkJsonObject;
MessageId: Integer;
Name: PWideChar;
DisplayName: PWideChar;
Size: Integer;
WasViewed: Boolean;
Location: PWideChar;
UserName: PWideChar;
AccountId: Integer;
AccountName: PWideChar;
AccountTitle: PWideChar;
FirstName: PWideChar;
LastName: PWideChar;
LastSentOn: PWideChar;
SendCount: Integer;
IsFavorite: Boolean;
Id: Integer;
FromUserId: Integer;
FromMailBoxId: Integer;
FromUserType: Integer;
FromUserName: PWideChar;
FromUserFullName: PWideChar;
FromUserAccountName: PWideChar;
FromUserAccountTitle: PWideChar;
ToUserId: Integer;
ToUserType: Integer;
ToUserMailboxId: PWideChar;
ToUserName: PWideChar;
ToUserFullName: PWideChar;
EmailAddress: PWideChar;
Password: PWideChar;
Subject: PWideChar;
PatientMedTunnelId: PWideChar;
Body: PWideChar;
DateReceived: PWideChar;
DisplayDateReceived: PWideChar;
ViewCount: Integer;
ViewedOn: PWideChar;
AttachmentCount: Integer;
Status: Integer;
ParentMessageId: Integer;
DistributionListId: Integer;
DistributionListName: PWideChar;
BodyHistory: PWideChar;
ReadReceiptCallbackUrl: PWideChar;
SendGlobalNotifications: Boolean;
i: Integer;
count_i: Integer;

begin
success := False;

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

http := CkHttp_Create();

// 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"
//         -F "file1=@qa_data/jpg/starfish.jpg"

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

req := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,'POST');
CkHttpRequest_putPath(req,'/MedTunnelMsg/api/Message/SendMessage');
CkHttpRequest_putContentType(req,'multipart/form-data');

CkHttpRequest_AddParam(req,'ApplicationId','yourApplicationId');
CkHttpRequest_AddParam(req,'LocationId','yourLocationId');
CkHttpRequest_AddParam(req,'MedTunnelId','yourMedTunnelId');
CkHttpRequest_AddParam(req,'MedTunnelPassword','yourMedTunnelPassword');
CkHttpRequest_AddParam(req,'To','recipientsMedTunnelId');
CkHttpRequest_AddParam(req,'Body','Test');

success := CkHttpRequest_AddFileForUpload2(req,'file1','qa_data/jpg/starfish.jpg','application/octet-stream');

CkHttpRequest_AddHeader(req,'Expect','100-continue');

resp := CkHttpResponse_Create();
success := CkHttp_HttpSReq(http,'server.medtunnel.com',443,True,req,resp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

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

respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttpResponse__header(resp));
    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 := CkJsonObject_IntOf(jResp,'ReturnCode');
ReturnCodeText := CkJsonObject__stringOf(jResp,'ReturnCodeText');
Data := CkJsonObject__stringOf(jResp,'Data');

// Load the Data into another JSON object and parse..
jsonData := CkJsonObject_Create();
CkJsonObject_Load(jsonData,Data);
CkJsonObject_putEmitCompact(jsonData,False);
Memo1.Lines.Add(CkJsonObject__emit(jsonData));

// {
//   "Id": 989448,
//   "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:48:22 PM",
//   "DisplayDateReceived": "04/29/2021  2:48 PM",
//   "ViewCount": 0,
//   "ViewedOn": "",
//   "AttachmentCount": 1,
//   "AttachmentNames": [
//     {
//       "MessageId": 989448,
//       "Id": 424857,
//       "Name": "starfish.jpg.35910fe9-4118-414c-a845-4d092ca6e784",
//       "DisplayName": "starfish.jpg",
//       "Size": 6229,
//       "WasViewed": false,
//       "ViewedOn": "",
//       "ViewCount": 0,
//       "Location": "Default"
//     }
//   ],
//   "AllRecipients": [
//     {
//       "Id": 989448,
//       "UserName": "...",
//       "AccountId": 0,
//       "AccountName": "...",
//       "AccountTitle": "",
//       "FirstName": "...",
//       "LastName": "...",
//       "EmailAddress": "",
//       "LastSentOn": "",
//       "SendCount": 0,
//       "IsFavorite": false
//     }
//   ],
//   "Status": 0,
//   "ParentMessageId": 989448,
//   "DistributionListId": 0,
//   "DistributionListName": "",
//   "BodyHistory": "",
//   "ReadReceiptCallbackUrl": null,
//   "SendGlobalNotifications": false
// }

Id := CkJsonObject_IntOf(jsonData,'Id');
FromUserId := CkJsonObject_IntOf(jsonData,'FromUserId');
FromMailBoxId := CkJsonObject_IntOf(jsonData,'FromMailBoxId');
FromUserType := CkJsonObject_IntOf(jsonData,'FromUserType');
FromUserName := CkJsonObject__stringOf(jsonData,'FromUserName');
FromUserFullName := CkJsonObject__stringOf(jsonData,'FromUserFullName');
FromUserAccountName := CkJsonObject__stringOf(jsonData,'FromUserAccountName');
FromUserAccountTitle := CkJsonObject__stringOf(jsonData,'FromUserAccountTitle');
ToUserId := CkJsonObject_IntOf(jsonData,'ToUserId');
ToUserType := CkJsonObject_IntOf(jsonData,'ToUserType');
ToUserMailboxId := CkJsonObject__stringOf(jsonData,'ToUserMailboxId');
ToUserName := CkJsonObject__stringOf(jsonData,'ToUserName');
ToUserFullName := CkJsonObject__stringOf(jsonData,'ToUserFullName');
EmailAddress := CkJsonObject__stringOf(jsonData,'EmailAddress');
Password := CkJsonObject__stringOf(jsonData,'Password');
Subject := CkJsonObject__stringOf(jsonData,'Subject');
PatientMedTunnelId := CkJsonObject__stringOf(jsonData,'PatientMedTunnelId');
Body := CkJsonObject__stringOf(jsonData,'Body');
DateReceived := CkJsonObject__stringOf(jsonData,'DateReceived');
DisplayDateReceived := CkJsonObject__stringOf(jsonData,'DisplayDateReceived');
ViewCount := CkJsonObject_IntOf(jsonData,'ViewCount');
ViewedOn := CkJsonObject__stringOf(jsonData,'ViewedOn');
AttachmentCount := CkJsonObject_IntOf(jsonData,'AttachmentCount');
Status := CkJsonObject_IntOf(jsonData,'Status');
ParentMessageId := CkJsonObject_IntOf(jsonData,'ParentMessageId');
DistributionListId := CkJsonObject_IntOf(jsonData,'DistributionListId');
DistributionListName := CkJsonObject__stringOf(jsonData,'DistributionListName');
BodyHistory := CkJsonObject__stringOf(jsonData,'BodyHistory');
ReadReceiptCallbackUrl := CkJsonObject__stringOf(jsonData,'ReadReceiptCallbackUrl');
SendGlobalNotifications := CkJsonObject_BoolOf(jsonData,'SendGlobalNotifications');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonData,'AttachmentNames');
while i < count_i do
  begin
    CkJsonObject_putI(jsonData,i);
    MessageId := CkJsonObject_IntOf(jsonData,'AttachmentNames[i].MessageId');
    Id := CkJsonObject_IntOf(jsonData,'AttachmentNames[i].Id');
    Name := CkJsonObject__stringOf(jsonData,'AttachmentNames[i].Name');
    DisplayName := CkJsonObject__stringOf(jsonData,'AttachmentNames[i].DisplayName');
    Size := CkJsonObject_IntOf(jsonData,'AttachmentNames[i].Size');
    WasViewed := CkJsonObject_BoolOf(jsonData,'AttachmentNames[i].WasViewed');
    ViewedOn := CkJsonObject__stringOf(jsonData,'AttachmentNames[i].ViewedOn');
    ViewCount := CkJsonObject_IntOf(jsonData,'AttachmentNames[i].ViewCount');
    Location := CkJsonObject__stringOf(jsonData,'AttachmentNames[i].Location');
    i := i + 1;
  end;

i := 0;
count_i := CkJsonObject_SizeOfArray(jsonData,'AllRecipients');
while i < count_i do
  begin
    CkJsonObject_putI(jsonData,i);
    Id := CkJsonObject_IntOf(jsonData,'AllRecipients[i].Id');
    UserName := CkJsonObject__stringOf(jsonData,'AllRecipients[i].UserName');
    AccountId := CkJsonObject_IntOf(jsonData,'AllRecipients[i].AccountId');
    AccountName := CkJsonObject__stringOf(jsonData,'AllRecipients[i].AccountName');
    AccountTitle := CkJsonObject__stringOf(jsonData,'AllRecipients[i].AccountTitle');
    FirstName := CkJsonObject__stringOf(jsonData,'AllRecipients[i].FirstName');
    LastName := CkJsonObject__stringOf(jsonData,'AllRecipients[i].LastName');
    EmailAddress := CkJsonObject__stringOf(jsonData,'AllRecipients[i].EmailAddress');
    LastSentOn := CkJsonObject__stringOf(jsonData,'AllRecipients[i].LastSentOn');
    SendCount := CkJsonObject_IntOf(jsonData,'AllRecipients[i].SendCount');
    IsFavorite := CkJsonObject_BoolOf(jsonData,'AllRecipients[i].IsFavorite');
    i := i + 1;
  end;

CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
CkJsonObject_Dispose(jsonData);

end;