0

I have been using clickatell service last couple of weeks sending SMS each morning to a number of mobile numbers, no problems until today:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream.

I have tried sending from inside the corporate network, and outside of it too, even tried from mobile data connection, get the same response every time.

I checked my clickatell account, and it still has plenty of credit, and the integration is switched 'on'..

any ideas what is going wrong?

Andie
  • 413
  • 1
  • 6
  • 17

2 Answers2

0

After the lack of response from clickatell, i did some digging around and found this thread...

"The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate

As I am using .net 4, I tried adding this line of code to the Rest Class I got from clickatell...

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

Adding this code appears to have made the system work again..

so, if you are using .net4 and the REST api, this is the code you need:

class Rest
{
    //This takes the API Key and JSON array of data and posts it to the Message URL to send the SMS's
    public static string Post(string Token, string json)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://platform.clickatell.com/messages");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Accept = "application/json";
        httpWebRequest.PreAuthenticate = true;
        httpWebRequest.Headers.Add("Authorization", Token);

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            return result;
        }
   }
}

hope this helps someone else....

Andie
  • 413
  • 1
  • 6
  • 17
0

Yes, the SecurityProtocol must be Transport Layer Security 1.2 (3072 it's Tls12 SecurityProtocolType enumeration value).

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

With .net 4.6 Microsoft has upgraded security restrictions on ssl.

If you want more, read this: ServicePointManager o SslStream APIs in .net 4.6

Sorry, this article is in Italian, I didn't find the English version.