Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

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

    CkHttp http = new CkHttp();

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

    CkHttpRequest req = new CkHttpRequest();
    req.put_HttpVerb("POST");
    req.put_Path("/MedTunnelMsg/api/Message/SendMessage");
    req.put_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");

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpSReq("server.medtunnel.com",443,true,req,resp);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);
    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    Log.i(TAG, "Response Body:");
    Log.i(TAG, jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, resp.header());
        Log.i(TAG, "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..
    CkJsonObject jsonData = new CkJsonObject();
    jsonData.Load(Data);
    jsonData.put_EmitCompact(false);
    Log.i(TAG, 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
    //  }

    int MessageId;
    String Name;
    String DisplayName;
    int Size;
    boolean WasViewed;
    String Location;
    String UserName;
    int AccountId;
    String AccountName;
    String AccountTitle;
    String FirstName;
    String LastName;
    String LastSentOn;
    int SendCount;
    boolean IsFavorite;

    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.put_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;
        }

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


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}