ALT.SMS.SmppClient

Пример использования отправки СМС сообщений по протоколу Smpp используя ALT.SMS.SmppClient.dll и C#.NET

public static bool SendSMS(string phone, string message)
        {
            bool result = false;
            try
            {
                if (phone.Length == 10)
                {
                    phone = String.Concat("7", phone);
                }

                if (phone.Length != 11)
                {
                    return false;
                }

                string host = "127,0,0,1";
                int port = 911;
                byte source_ton = 5;
                byte source_npi = 0;
                string source_address = "some_adress";
                byte target_ton = 1;
                byte target_npi = 1;
                string system_id = "user";
                string password = "pa$$w0rd";


                var client = new SmppClient();
                client.evError += new SmppClient.ErrorEventHandler(client_evError);

                client.AddrNpi = 0;
                client.AddrTon = 0;
                client.Timeout = 120000;
                client.NeedEnquireLink = false;

                if (client.Connect(host, port))
                {
                    try
                    {
                        var bindResp = client.Bind(system_id, password);
                        if (bindResp.Status == CommandStatus.ESME_ROK || bindResp.Status == CommandStatus.ESME_RALYBND)
                        {
                            var req = client.PrepareSubmit(SubmitMode.ShortMessage, source_ton, source_npi, source_address, target_ton, target_npi, phone, DataCodings.UCS2, message);
                            var submitResp = client.Submit(req);
                            var unbindResp = client.UnBind();

                            if (0 >= submitResp.Length)
                            {
                                throw new ApplicationException("Ответ от сервера не получен.");
                            }

                            if (submitResp[0].Status != CommandStatus.ESME_ROK)
                            {
                              LogError(String.Format("Неожиданный ответ с сервера: {0}", submitResp[0].Status.ToString()), null);
                            }

                            result = submitResp[0].Status == CommandStatus.ESME_ROK;
                        }
                        else
                        {
                            LogError(String.Format("Ошибка привязки. Статус: {0} Описание: {1}", ((int)bindResp.Status).ToString(), Enum.GetName(typeof(CommandStatus), bindResp.Status)), null);
                        }
                    }
                    finally
                    {
                        client.Disconnect();
                    }
                }
                else
                {
                    LogError(String.Format("Невозможно соединиться с сервером смс ({0}:{1})", host,port), null);
                }
            }
            catch (Exception ex)
            {
                LogError("Общая ошибка.", ex);
                return false;
            }

            return result;
        }

        static void client_evError(object sender, string Comment, Exception message)
        {
            //some log
            LogError(Comment, message);
        }

Добавить комментарий

Loading