Sample code for 30+ languages & platforms
B4X

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 B4X Downloads

B4X
Dim success As Boolean = False

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

Dim http As ChilkatHttp
http.Initialize("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"
'          -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

Dim req As ChilkatHttpRequest
req.Initialize
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")

Dim resp As ChilkatHttpResponse
resp.Initialize
success = http.HttpSReq("server.medtunnel.com", 443, True, req, resp)
If success = False Then
    Log(http.LastErrorText)
    Return
End If


Dim sbResponseBody As ChilkatStringBuilder
sbResponseBody.Initialize
resp.GetBodySb(sbResponseBody)
Dim jResp As ChilkatJsonObject
jResp.Initialize
jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = False

Log("Response Body:")
Log(jResp.Emit)

Dim respStatusCode As Int = resp.StatusCode
Log("Response Status Code = " & respStatusCode)
If respStatusCode >= 400 Then
    Log("Response Header:")
    Log(resp.Header)
    Log("Failed.")
    Return
End If


'  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



Dim ReturnCode As Int = jResp.IntOf("ReturnCode")
Dim ReturnCodeText As String = jResp.StringOf("ReturnCodeText")
Dim Data As String = jResp.StringOf("Data")

'  Load the Data into another JSON object and parse..
Dim jsonData As ChilkatJsonObject
jsonData.Initialize
jsonData.Load(Data)
jsonData.EmitCompact = False
Log(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
'  }



Dim MessageId As Int
Dim Name As String
Dim DisplayName As String
Dim Size As Int
Dim WasViewed As Boolean
Dim Location As String
Dim UserName As String
Dim AccountId As Int
Dim AccountName As String
Dim AccountTitle As String
Dim FirstName As String
Dim LastName As String
Dim LastSentOn As String
Dim SendCount As Int
Dim IsFavorite As Boolean

Dim Id As Int = jsonData.IntOf("Id")
Dim FromUserId As Int = jsonData.IntOf("FromUserId")
Dim FromMailBoxId As Int = jsonData.IntOf("FromMailBoxId")
Dim FromUserType As Int = jsonData.IntOf("FromUserType")
Dim FromUserName As String = jsonData.StringOf("FromUserName")
Dim FromUserFullName As String = jsonData.StringOf("FromUserFullName")
Dim FromUserAccountName As String = jsonData.StringOf("FromUserAccountName")
Dim FromUserAccountTitle As String = jsonData.StringOf("FromUserAccountTitle")
Dim ToUserId As Int = jsonData.IntOf("ToUserId")
Dim ToUserType As Int = jsonData.IntOf("ToUserType")
Dim ToUserMailboxId As String = jsonData.StringOf("ToUserMailboxId")
Dim ToUserName As String = jsonData.StringOf("ToUserName")
Dim ToUserFullName As String = jsonData.StringOf("ToUserFullName")
Dim EmailAddress As String = jsonData.StringOf("EmailAddress")
Dim Password As String = jsonData.StringOf("Password")
Dim Subject As String = jsonData.StringOf("Subject")
Dim PatientMedTunnelId As String = jsonData.StringOf("PatientMedTunnelId")
Dim Body As String = jsonData.StringOf("Body")
Dim DateReceived As String = jsonData.StringOf("DateReceived")
Dim DisplayDateReceived As String = jsonData.StringOf("DisplayDateReceived")
Dim ViewCount As Int = jsonData.IntOf("ViewCount")
Dim ViewedOn As String = jsonData.StringOf("ViewedOn")
Dim AttachmentCount As Int = jsonData.IntOf("AttachmentCount")
Dim Status As Int = jsonData.IntOf("Status")
Dim ParentMessageId As Int = jsonData.IntOf("ParentMessageId")
Dim DistributionListId As Int = jsonData.IntOf("DistributionListId")
Dim DistributionListName As String = jsonData.StringOf("DistributionListName")
Dim BodyHistory As String = jsonData.StringOf("BodyHistory")
Dim ReadReceiptCallbackUrl As String = jsonData.StringOf("ReadReceiptCallbackUrl")
Dim SendGlobalNotifications As Boolean = jsonData.BoolOf("SendGlobalNotifications")
Dim i As Int = 0
Dim count_i As Int = jsonData.SizeOfArray("AttachmentNames")
Do While i < count_i
    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
Loop
i = 0
count_i = jsonData.SizeOfArray("AllRecipients")
Do While i < count_i
    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
Loop