Sample code for 30+ languages & platforms
Java

MedTunnel: Send Message Text Only

See more MedTunnel Examples

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

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

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

        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setPath("/MedTunnelMsg/api/Message/SendMessage");
        req.setContentType("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");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpSReq("server.medtunnel.com", 443, true, req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

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

        int ReturnCode = jResp.intOf("ReturnCode");
        String ReturnCodeText = jResp.stringOf("ReturnCodeText");
        String Data = jResp.stringOf("Data");

        // Load the Data into another JSON object and parse..
        JsonObject jsonData = new JsonObject();
        jsonData.load(Data);
        jsonData.setEmitCompact(false);
        System.out.println(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
        // }
        //

        String UserName = "";
        int AccountId = 0;
        String AccountName = "";
        String AccountTitle = "";
        String FirstName = "";
        String LastName = "";
        String LastSentOn = "";
        int SendCount = 0;
        boolean IsFavorite = false;

        int Id = jsonData.intOf("Id");
        int FromUserId = jsonData.intOf("FromUserId");
        int FromMailBoxId = jsonData.intOf("FromMailBoxId");
        int FromUserType = jsonData.intOf("FromUserType");
        String FromUserName = jsonData.stringOf("FromUserName");
        String FromUserFullName = jsonData.stringOf("FromUserFullName");
        String FromUserAccountName = jsonData.stringOf("FromUserAccountName");
        String FromUserAccountTitle = jsonData.stringOf("FromUserAccountTitle");
        int ToUserId = jsonData.intOf("ToUserId");
        int ToUserType = jsonData.intOf("ToUserType");
        String ToUserMailboxId = jsonData.stringOf("ToUserMailboxId");
        String ToUserName = jsonData.stringOf("ToUserName");
        String ToUserFullName = jsonData.stringOf("ToUserFullName");
        String EmailAddress = jsonData.stringOf("EmailAddress");
        String Password = jsonData.stringOf("Password");
        String Subject = jsonData.stringOf("Subject");
        String PatientMedTunnelId = jsonData.stringOf("PatientMedTunnelId");
        String Body = jsonData.stringOf("Body");
        String DateReceived = jsonData.stringOf("DateReceived");
        String DisplayDateReceived = jsonData.stringOf("DisplayDateReceived");
        int ViewCount = jsonData.intOf("ViewCount");
        String ViewedOn = jsonData.stringOf("ViewedOn");
        int AttachmentCount = jsonData.intOf("AttachmentCount");
        int Status = jsonData.intOf("Status");
        int ParentMessageId = jsonData.intOf("ParentMessageId");
        int DistributionListId = jsonData.intOf("DistributionListId");
        String DistributionListName = jsonData.stringOf("DistributionListName");
        String BodyHistory = jsonData.stringOf("BodyHistory");
        String ReadReceiptCallbackUrl = jsonData.stringOf("ReadReceiptCallbackUrl");
        boolean SendGlobalNotifications = jsonData.boolOf("SendGlobalNotifications");
        int i = 0;
        int count_i = jsonData.sizeOfArray("AttachmentNames");
        while (i < count_i) {
            jsonData.setI(i);
            i = i + 1;
        }

        i = 0;
        count_i = jsonData.sizeOfArray("AllRecipients");
        while (i < count_i) {
            jsonData.setI(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;
        }
    }
}