private async Task Disconnect() { _log.Info("Disconnect from SMPP server"); if (_client.Status == ConnectionStatus.Bound) { await UnBind(); } if (_client.Status == ConnectionStatus.Open) { await _client.Disconnect(); } }
// <SendHelloWorld> public static async Task SendHelloWorld() { using (SmppServer server = new SmppServer(new IPEndPoint(IPAddress.Any, 7777))) { server.Start(); using (SmppClient client = new SmppClient()) { if (await client.Connect("localhost", 7777)) { BindResp bindResp = await client.Bind("1", "2"); if (bindResp.Header.Status == CommandStatus.ESME_ROK) { var submitResp = await client.Submit( SMS.ForSubmit() .From("111") .To("222") .Coding(DataCodings.UCS2) .Text("Hello World!")); if (submitResp.All(x => x.Header.Status == CommandStatus.ESME_ROK)) { client.Logger.Info("Message has been sent."); } } await client.Disconnect(); } } } }
public static async Task Run() { // <Sample> using (SmppServer server = new SmppServer(new IPEndPoint(IPAddress.Any, 7777))) { server.EnabledSslProtocols = SslProtocols.Tls12; server.ServerCertificate = new X509Certificate2("server_certificate.p12", "cert_password"); server.Start(); server.evClientConnected += (sender, client) => { var clientCertificate = client.ClientCertificate; //You can validate client certificate and disconnect if it is not valid. }; using (SmppClient client = new SmppClient()) { client.EnabledSslProtocols = SslProtocols.Tls12; //if required you can be authenticated with client certificate client.ClientCertificates.Add(new X509Certificate2("client_certificate.p12", "cert_password")); if (await client.Connect("localhost", 7777)) { BindResp bindResp = await client.Bind("username", "password"); if (bindResp.Header.Status == CommandStatus.ESME_ROK) { var submitResp = await client.Submit( SMS.ForSubmit() .From("111") .To("436641234567") .Coding(DataCodings.UCS2) .Text("Hello World!")); if (submitResp.All(x => x.Header.Status == CommandStatus.ESME_ROK)) { client.Logger.Info("Message has been sent."); } } await client.Disconnect(); } } } //</Sample> }
public static async Task SendSms(string phoneNumber, string smsText) { string filePath = ConfigurationManager.AppSettings.Get("SMPPLogPath"); LogManager.SetLoggerFactory(name => new FileLogger(filePath, LogLevel.All)); using (SmppClient client = new SmppClient()) { try { if (await client.Connect(new DnsEndPoint("smpp.server", 7777, AddressFamily.InterNetwork))) { BindResp bindResp = await client.Bind("username", "password"); if (bindResp.Header.Status == CommandStatus.ESME_ROK) { var submitResp = await client.Submit( SMS.ForSubmit() .From("short code") .To(phoneNumber) .Coding(DataCodings.UCS2) .Text(smsText)); if (submitResp.All(x => x.Header.Status == CommandStatus.ESME_ROK)) { client.Logger.Info("Message has been sent."); } } await client.Disconnect(); } } catch (Exception ex) { client.Logger.Error("Failed send message", ex); } } }
/// <summary> Called to connect to the server </summary> private void PerformConnectClient() { for (;;) { try { // Wait to be told to connect ConnectEvent.WaitOne(); // Are we shutting down if (Disposed == true) { WriteLog("ESMEConnection : PerformConnectClient : Info : Killing Thread"); break; } WriteLog("ESMEConnection : PerformConnectClient : Info : Attempting To Connect"); // Connect to the server if (Connect()) { if (Bind()) { // We are good to go ConnectEvent.Reset(); } else { WriteLog("ESMEConnection : PerformConnectClient : Info : Dropping the Connection"); // Drop the connection Client.Disconnect(); WriteLog("ESMEConnection : PerformConnectClient : Info : Sleep For 5 Seconds and try again"); // Wait five second an try again Thread.Sleep(5000); WriteLog("ESMEConnection : PerformConnectClient : Info : Try again"); ConnectEvent.Set(); } } else { // Wait five second an try again Thread.Sleep(5000); ConnectEvent.Set(); } } catch (Exception exception) { WriteLog("ESMEConnection : PerformConnectClient : Info : Dropping the Connection : {0}", exception.Message); if (Client == null) { WriteLog("ESMEConnection : PerformConnectClient : Info : Killing Thread"); break; } // Drop the connection Client.Disconnect(); WriteLog("ESMEConnection : PerformConnectClient : Info : Sleep For 5 Seconds and try again"); // Wait five second an try again Thread.Sleep(5000); WriteLog("ESMEConnection : PerformConnectClient : Info : Try again"); ConnectEvent.Set(); } } }