Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpRequest,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  req: THttpRequest;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  ReturnCode: Integer;
  ReturnCodeText: string;
  Data: string;
  jsonData: TJsonObject;
  MessageId: Integer;
  Name: string;
  DisplayName: string;
  Size: Integer;
  WasViewed: Boolean;
  Location: string;
  UserName: string;
  AccountId: Integer;
  AccountName: string;
  AccountTitle: string;
  FirstName: string;
  LastName: string;
  LastSentOn: string;
  SendCount: Integer;
  IsFavorite: Boolean;
  Id: Integer;
  FromUserId: Integer;
  FromMailBoxId: Integer;
  FromUserType: Integer;
  FromUserName: string;
  FromUserFullName: string;
  FromUserAccountName: string;
  FromUserAccountTitle: string;
  ToUserId: Integer;
  ToUserType: Integer;
  ToUserMailboxId: string;
  ToUserName: string;
  ToUserFullName: string;
  EmailAddress: string;
  Password: string;
  Subject: string;
  PatientMedTunnelId: string;
  Body: string;
  DateReceived: string;
  DisplayDateReceived: string;
  ViewCount: Integer;
  ViewedOn: string;
  AttachmentCount: Integer;
  Status: Integer;
  ParentMessageId: Integer;
  DistributionListId: Integer;
  DistributionListName: string;
  BodyHistory: string;
  ReadReceiptCallbackUrl: string;
  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 := THttp.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 := THttpRequest.Create;
  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');

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

  req.AddHeader('Expect','100-continue');

  resp := THttpResponse.Create;
  success := http.HttpSReq('server.medtunnel.com',443,True,req,resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);
  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('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 := TJsonObject.Create;
  jsonData.Load(Data);
  jsonData.EmitCompact := False;
  WriteLn(jsonData.Emit());

  //  {
  //    "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 := 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;
      MessageId := jsonData.IntOf('AttachmentNames[i].MessageId');
      Id := jsonData.IntOf('AttachmentNames[i].Id');
      Name := jsonData.StringOf('AttachmentNames[i].Name');
      DisplayName := jsonData.StringOf('AttachmentNames[i].DisplayName');
      Size := jsonData.IntOf('AttachmentNames[i].Size');
      WasViewed := jsonData.BoolOf('AttachmentNames[i].WasViewed');
      ViewedOn := jsonData.StringOf('AttachmentNames[i].ViewedOn');
      ViewCount := jsonData.IntOf('AttachmentNames[i].ViewCount');
      Location := jsonData.StringOf('AttachmentNames[i].Location');
      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;



  http.Free;
  req.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.Free;
  jsonData.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.