Esempio n. 1
0
        private void SendMmsSample()
        {
            // First instantiate the SendReq PDU we will be turning into a byte array that
            // will be ultimately sent to the MMS service provider.
            SendReq sendRequestPdu = new SendReq();

            // Set the recipient number of our MMS
            sendRequestPdu.AddTo(new EncodedStringValue("555-555-5555"));
            // Instantiate the body of our MMS
            PduBody pduBody = new PduBody();
            // Attach a sample image to our MMS body
            Stream       sampleImageStream = this.Assets.Open(SampleImageFileName);
            MemoryStream imageMemoryStream = new MemoryStream();

            sampleImageStream.CopyTo(imageMemoryStream);
            byte[]  sampleImageData    = imageMemoryStream.ToArray();
            PduPart sampleImagePduPart = new PduPart();

            sampleImagePduPart.SetData(sampleImageData);
            sampleImagePduPart.SetContentType(new EncodedStringValue("image/png").GetTextString());
            sampleImagePduPart.SetFilename(new EncodedStringValue(SampleImageFileName).GetTextString());
            pduBody.AddPart(sampleImagePduPart);
            // Set the body of our MMS
            sendRequestPdu.Body = pduBody;
            // Finally, generate the byte array to send to the MMS provider
            PduComposer composer = new PduComposer(sendRequestPdu);

            byte[] pduData       = composer.Make();
            int    pduDataLength = pduData.Length;

            Toast.MakeText(this, $"Successfully composed a PDU byte array of size: {pduDataLength.ToString("N0")} bytes", ToastLength.Long).Show();
        }
        public byte[] GetMMSPduData(string phone, string imgFilePath, string txtMessage)
        {
            Context ctx = AndroidGlobals.MainActivity;

            byte[] pduData = null;

            try
            {
                if (!string.IsNullOrEmpty(phone) && (!string.IsNullOrEmpty(imgFilePath) || !string.IsNullOrEmpty(txtMessage)))
                {
                    ///////////////////PDU Management Method///////////////////////////////////////////////////////

                    // First instantiate the PDU we will be turning into a byte array that
                    // will be ultimately sent to the MMS service provider.
                    // In this case, we are using a SendReq PDU type.
                    SendReq sendReqPdu = new SendReq();
                    // Set the recipient number of our MMS
                    sendReqPdu.AddTo(new EncodedStringValue(phone));
                    // Instantiate the body of our MMS
                    PduBody pduBody = new PduBody();

                    // Add any text message data
                    if (!string.IsNullOrEmpty(txtMessage))
                    {
                        PduPart txtPart = new PduPart();
                        txtPart.SetData(Encoding.ASCII.GetBytes(txtMessage));
                        txtPart.SetContentType(new EncodedStringValue("text/plain").GetTextString());
                        txtPart.SetName(new EncodedStringValue("Message").GetTextString());
                        pduBody.AddPart(txtPart);
                    }

                    // add any image data
                    if (!string.IsNullOrEmpty(imgFilePath))
                    {
                        PduPart imgPart = new PduPart();

                        byte[] sampleImageData = GetFileBytes(imgFilePath);

                        imgPart.SetData(sampleImageData);
                        imgPart.SetContentType(new EncodedStringValue("image/png").GetTextString());
                        imgPart.SetFilename(new EncodedStringValue(System.IO.Path.GetFileName(imgFilePath)).GetTextString());
                        pduBody.AddPart(imgPart);
                    }

                    // Set the body of our MMS
                    sendReqPdu.Body = pduBody;
                    // Finally, generate the byte array to send to the MMS provider
                    PduComposer composer = new PduComposer(sendReqPdu);
                    pduData = composer.Make();
                }
            }
            catch (Exception ex)
            {
                MobileGlobals.AddNewException(ex);
                MobileGlobals.WriteExceptionLog();
            }

            return(pduData);
        }