title: Send sms using Mbn Api C#
body: <p>I'm looking for a way to send Sms throught MbnApi using :
  <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd323167(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/dd323167(v=vs.85).aspx</a>
  IMbnSms interface. But it's not working at all. 
  I could not have a sample project to see how to do it. I'm coding on C# .Net</p>
  
  <p>Thx</p>
  
  <pre><code>public  class NaErrorHandling
  {
     public Dictionary&lt;UInt32, string&gt; Hresults { get;private set; }
  
     public NaErrorHandling()
     {
         Hresults = new Dictionary&lt;UInt32, string&gt;();
         Init();
     }
  
     private void Init()
     {
         Hresults.Add(0, "S_OK");
         Hresults.Add(0x8054820a, "E_MBN_SIM_NOT_INSERTED");
         Hresults.Add(0x80070057, "E_INVALIDARG");
         Hresults.Add(0x80548202, "E_MBN_BAD_SIM");
  
         Hresults.Add(0x80548210, "E_MBN_PIN_REQUIRED");
         Hresults.Add(0x80548224, "E_MBN_SMS_MEMORY_FAILURE");
         Hresults.Add(0x80548226, "E_MBN_SMS_UNKNOWN_SMSC_ADDRESS");
         Hresults.Add(0x80548209, "E_MBN_SERVICE_NOT_ACTIVATED");
         Hresults.Add(0x80548225, "E_MBN_SMS_NETWORK_TIMEOUT");
         Hresults.Add(0x8054820d, "E_MBN_NOT_REGISTERED");
         Hresults.Add(0x80548223, "E_MBN_SMS_LANG_NOT_SUPPORTED");
         Hresults.Add(0x80548220, "E_MBN_SMS_ENCODING_NOT_SUPPORTED");
         Hresults.Add(0x80548228, "E_MBN_SMS_OPERATION_NOT_ALLOWED");
         Hresults.Add(0x80548229, "E_MBN_SMS_MEMORY_FULL");
         Hresults.Add(0X80070015, "ERROR_NOT_READY");
  
         Hresults.Add(1062, "ERROR_SERVICE_NOT_ACTIVE");
     }
     public string GetError(int error)
     {
         UInt32 uerror = (UInt32)error;
         if (Hresults.ContainsKey(uerror))
             return Hresults[uerror];
         return "Error Not Know";
     }
  }
  
   public class SmsResult : MbnApi.IMbnSmsEvents 
  {
      public void OnSetSmsConfigurationComplete(MbnApi.IMbnSms sms, uint requestID, int status)
      {
      }
  
      public void OnSmsConfigurationChange(MbnApi.IMbnSms sms)
      {
      }
  
      public void OnSmsDeleteComplete(MbnApi.IMbnSms sms, uint requestID, int status)
      {
      }
  
      public void OnSmsNewClass0Message(MbnApi.IMbnSms sms, MbnApi.MBN_SMS_FORMAT SmsFormat, object[] readMsgs)
      {
  
      }
  
      public void OnSmsReadComplete(MbnApi.IMbnSms sms, MbnApi.MBN_SMS_FORMAT SmsFormat, object[] readMsgs, bool moreMsgs, uint requestID, int status)
      {
          string messageShow = String.Empty;
          SMSPDULib.SMS rms= new SMSPDULib.SMS();
  
          foreach (var msgReceived in readMsgs)
          {
              var msg = msgReceived as IMbnSmsReadMsgPdu;
             messageShow +=  GetSms(msg.PduData) + "\n";
          }
          System.Windows.MessageBox.Show( messageShow);
      }
  
  
      private string GetSms(string pduSource)
      {
          SMSType smsType = SMSBase.GetSMSType(pduSource);
          string message = string.Empty;
          switch (smsType)
          {
              case SMSType.SMS:
                  SMS sms = new SMS();
                  SMS.Fetch(sms, ref pduSource);
                  message = sms.Message;
                  //Use instance of SMS class here
                  break;
              case SMSType.StatusReport:
                  SMSStatusReport statusReport = new SMSStatusReport();
                  SMSStatusReport.Fetch(statusReport, ref pduSource);
                  //Use instance of SMSStatusReport class here
                  break;
          }
          return message;
      }
      public void OnSmsSendComplete(MbnApi.IMbnSms sms, uint requestID, int status)
      {
          NaErrorHandling handling = new NaErrorHandling();
          var res = handling.GetError(status);
          UInt32 uStatus = (UInt32)status;
      }
  
      public void OnSmsStatusChange(MbnApi.IMbnSms sms)
      {
          throw new NotImplementedException();
      }
  }
  
  
  public void SendSms()
      {
          MbnConnectionManager mbnConnectionMgr = new MbnConnectionManager();
          IMbnConnectionManager connMgr = (IMbnConnectionManager)mbnConnectionMgr;
          try
          {
              IMbnConnection[] arrConn = connMgr.GetConnections();
              if (arrConn != null)
              {
                  String res = String.Empty;
                  foreach (var connection in arrConn)
                  {
                      res += String.Format("Connection ID : {0} | Interface Id : {1}\n", connection.ConnectionID, connection.InterfaceID);
                  }
                  //MessageBox.Show(res);
                  var conn = arrConn[0];
                  uint prId = 0;
                  //conn.Connect(MBN_CONNECTION_MODE.MBN_CONNECTION_MODE_PROFILE, "Bouygues Telecom", out prId);
                  MbnInterfaceManager mbnInterfaceMgr = new MbnInterfaceManager();
                  var mbnInterface = mbnInterfaceMgr as IMbnInterfaceManager;
                  var selectInterface = mbnInterface.GetInterface(conn.InterfaceID);
                  MBN_INTERFACE_CAPS caps =  selectInterface.GetInterfaceCapability();
  
                  var theCap =caps.smsCaps;// (uint)MBN_SMS_CAPS.MBN_SMS_CAPS_PDU_SEND;
                  IConnectionPointContainer icpc;
                  icpc = (IConnectionPointContainer)mbnInterfaceMgr;
  
                  Guid IID_IMbnConnectionEvents = typeof(IMbnSmsEvents).GUID;
                  IConnectionPoint icp;
                  icpc.FindConnectionPoint(ref IID_IMbnConnectionEvents, out icp);
  
                  SmsResult connEvtsSink = new SmsResult();
                  uint cookie;
                  icp.Advise(connEvtsSink, out cookie);
  
  
                  var sms = selectInterface as IMbnSms;
                  string phoneDest = "";
                  string pdu = String.Empty;
                  byte[] size = new byte[5];
  
                  uint requestId = 456;
                  MBN_SMS_STATUS_INFO smsStatusInfo = new MBN_SMS_STATUS_INFO();
                  smsStatusInfo.flag = 10;
                  try
                  {
                      IMbnSmsConfiguration smsConfig = sms.GetSmsConfiguration();
                      sms.GetSmsStatus(out smsStatusInfo);
  
                      var wtf = smsStatusInfo;
                      string serviceDatacenter = smsConfig.ServiceCenterAddress;
                      CreatePduSms(null, phoneDest, "Hello", out pdu, out size);
  
                      //var res1 = SMSPDULib.SMS.GetBytes(pdu, 16);
                      var total = SMSPDULib.SMS.GetBytes(pdu);
                      byte wantedSize =(byte) total.Length;
  
                      CreatePduSms(serviceDatacenter, phoneDest, "Hello", out pdu, out size);
                      //var byteArray = StringToByteArray(pdu);
                      //var str = System.Text.Encoding.UTF7.GetString(byteArray);
  
                      sms.SmsSendPdu(pdu, wantedSize, out requestId); 
                      //sms.SmsSendCdma(phoneDest, MBN_SMS_CDMA_ENCODING.MBN_SMS_CDMA_ENCODING_7BIT_ASCII, MBN_SMS_CDMA_LANG.MBN_SMS_CDMA_LANG_FRENCH, 2, GetBytes("Hi"), out requestId);
                  }
                  catch (Exception exp)
                  {
                      //MessageBox.Show("Cannot send Message : " + exp.Message);
                  }
  
              }
          }
          catch (Exception e)
          {
              //MessageBox.Show(e.Message + e.StackTrace);
          }
      }
  
      private void CreatePduSms(string servAddress, string phoneDest, string msg, out string pdu, out byte[] size)
      {
  
          SMSPDULib.SMS sms = new SMSPDULib.SMS();
          sms.Direction = SMSPDULib.SMSDirection.Submited;
          sms.PhoneNumber = "";
          if (servAddress != null)
          sms.ServiceCenterNumber = servAddress;
          sms.ValidityPeriod = new TimeSpan(4, 0, 0, 0);
          sms.Message = msg + DateTime.Now.ToString();
          pdu = sms.Compose(SMSPDULib.SMS.SMSEncoding._7bit);
          size = 0;
      }
  </code></pre>