Click or drag to resize

MessageControllerPost Method

Sends a new message to a list of recipients along with any specified attachments from the logged in User's account.

Namespace:  MedTunnelMsg.Controllers
Assembly:  MedTunnelMsg (in MedTunnelMsg.dll) Version: (
public Task<Result> Post()

Return Value

Type: TaskResult
Result.Data contains a Message object of the sent message. Result.ReturnCode values:
  • 0 = An unknown error occurred
  • 1 = Success
  • 6 = Required Item Missing: Message must contain text and/or at least one attachment
  • 7 = Message Password missing. This is returned when there is an email recipient and the password is missing
  • 8 = Recipient(s) not found - recipientnames. This is a MedTunnel recipient that is not found – 'recipientnames' will be a list of which recipients are not found. Message is not sent to any valid recipients
  • 9 = File too large: thefilename
  • 10 = Patient MedTunnel Id is not valid
The Request must be sent as a MimeMultiPartContent request body containing the required fields and any file attachments. A message must contain at least Message Text or one or more Files. You must login to MedTunnel with a valid Id and Password before calling this method.
  • Passed in the Request body. The Message contains the fields:
  • EmailAddress - The MedTunnel Id or email address of the recipient. Can be a comma seperated list, but if sending to an email address it must be the only recipient
  • Body - The message text
  • Password - If sending to an email address this will be the password required to view the message. If not specified and a default password exist it will be used
  • PatientMedTunnelId - MedTunnel Id of the patient sending the message (only required for messages from patients)
  • file1...filen - Optional File attachments
//namespaces to include
//using System;
//using System.Net;
//using System.Web.Script.Serialization;
//using System.IO;
//using System.Net.Http;
//using System.Net.Http.Headers;

string authKey = "";
string url;
string downloadString;
string dataToSend;
Result webResult;
JavaScriptSerializer serializer = new JavaScriptSerializer();

//if the session has expired then re-login
if (!SessionStillActive())
    authKey = Login();

//var provider = new MultipartMemoryStreamProvider();
//var content = new MultipartFormDataContent();

using (var client = new HttpClient())
    using (var content = new MultipartFormDataContent())
        MedTunnelMsg.Models.Message message = new MedTunnelMsg.Models.Message()
            EmailAddress = "jane@janesmedical",
            Body = "This can be any message text",
            Password = "",                //only required for messages sent to an e-mail address
            PatientMedTunnelId = ""        //optional - only required for messages sent from a patient account

        //add the Message object to the request
        content.Add(new StringContent(serializer.Serialize(message)));

        //add the file(s) selected in the web form to the request
        foreach (UploadedFile postedFile in FileUpload1.UploadedFiles)
            //read the file contents into a byte array
            BinaryReader br = new BinaryReader(postedFile.InputStream);
            var fileContent = new ByteArrayContent(br.ReadBytes(Convert.ToInt32(postedFile.ContentLength)));

            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                FileName = postedFile.FileName


        url = "";
        client.DefaultRequestHeaders.Add("Authorization", authKey);
        var sendResult = client.PostAsync(url, content).Result;

        webResult = serializer.Deserialize<Result>(sendResult.Content.ReadAsStringAsync().Result);

        if (webResult.ReturnCode != 1)
            throw new Exception("SendFailed");
Message text only
curl -X POST -k 
        -H "Authorization: PutAuthorizationTokenHere"
        -F "EmailAddress =recipientsMedTunnelId" 
        -F "Body=Test of SendMessage"
        -F "Password=A password is only needed if sent to an email address "
        -F"PatientMedTunnelId= Optional-Required if sent from a patient Account "

Message with a file attachment
curl -X POST -k 
        -H "Authorization: PutAuthorizationTokenHere"
        -F "EmailAddress =recipientsMedTunnelId" 
        -F "Body=Test of SendMessage"
        -F "Password=A password is only needed if sent to an email address "
        -F"PatientMedTunnelId= Optional-Required if sent from a patient Account "
        -F "file1=@C:\temp\Desert.jpg"
See Also