使用NowSMS Gateway来接收发送短信和彩信

硬件准备:
GPRS Modem,不同的运营商波段可能不一样,比如Cingular是850/1900 MHz,而China Mobile是900/1800 MHz,购买时要注意.把SIM card插入Modem, SIM要开通GPRS service,把Modem接到COM Port,完成硬件准备。
软件准备:
下载SMS Gateway,我用的是NowSMS Gateway,觉得还可以,支持多种语言字符集,在http://www.nowsms.com/可以下载60天试用版本,网上也有其他的Gateway.

添加GPRS Modem:
由于不同Modem,NowSMS Gateway不一定都能认识,以至于在配置MMS的时候不能认出COM port,我用的WaveCOM的Modem,配彩信时救认不出.
所以要先通过在Windows里添加标准Modem以便NowSMS gateway识别.
Control Panel-->Phone and Modem options-->Modems:
Select Add, follow the wizard, and select appropriate COM port, GPRS Modem could be recognized.


怎样来配置NowSMS Gateway,大部分在其网站有如何配置说明,这里主要讲一下有几个要注意的地方,特别是配置MMS的时候。
如何配置短信可以参考网站的资料(http://www.nowsms.com/documentation/ProductDocumentation/2_way_sms_support.htm),这里就不多说

配置Gateway通过GPRS Modem收发彩信(http://www.nowsms.com/documentation/ProductDocumentation/2_way_mms_support.htm):
大部分内容在里面都有,这里我只提一下通过GPRS Modem来收发彩信,还可以通过网络连接来收发彩信,不过不同的Operator有一定的限制.

配置GPRS Modem接收彩信:
如果Modem加好了没问题,你可以在NowSMS Gateway的SMSC面板 Add的时候就能看到你刚才加的Modem,选择这个Modem就可以了,然后可以按一下Test可以测试一下Modem是否work.
接着可以在Properties里配置MMS Setting.
选择相应的Operator,选择Use Specific Network Connection(GPRS Modem),这时可以在Network Connection里找到刚才添加的Modem,选择即可,然后可以点击Test Connection测试连接是否工作
这样你就可以通过GPRS Modem接收彩信了,如果你配置为存在制定目录的话,接收的彩信将会存在那个目录,每个彩信会有一个.hdr的头文件,和一个放彩信内容
的子目录.

配置GPRS Modem发送彩信:
在MMSC Routing里面选择Add,添加一个Additional Routes,在配置MMS Outbound Routing的Dialog里的Route message to VASP via:选项里选MM1,选择Use Specific Network Connection(GPRS Modem),在Network Connection下拉框里选择刚才添加的Modem连接,OK后,把Default Route设置为这个新添的Route就可以了.

收到彩信后你可以写自己的程序去处理,NowSMS本身也支持配置Php来处理(http://www.nowsms.com/support/bulletins/tb-nowsms-016.htm).
如何用程序来发送SMS和MMS, NowSMS Gateway提供一种Web Menu Interface(http://www.nowsms.com/documentation/ProductDocumentation/the_web_interface/index.htm),你只要把要发的信息,和手机号提交给web server就可以了,接口比较方便易用, SMS用GET方式,MMS用的是POST方式.

发送短信函数(C#):

        private void SendSMSMessage(String strMobile, String strText)
        {
            try
            {
                String request = "http://127.0.0.1:8800/Send%20Text%20Message.htm?PhoneNumber=" + strMobile + "&Text=" + strText + "&InfoCharCounter=&PID=&DCS=&Submit=Submit";

                Uri myUri = new Uri(request);
                // Create a new request to the above mentioned URL.   
                WebRequest myWebRequest = WebRequest.Create(myUri);
                // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
                WebResponse myWebResponse = myWebRequest.GetResponse();
            }
            catch (Exception)
            {
                throw;
            }
        }

发送彩信函数(C#):

        protected byte[] CreateTextData(string boundary, string textFormName, string strText)
        {
            // Build up the post message header.
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(boundary);
            sb.Append("/r/n");
            sb.Append("Content-Disposition: form-data; name=/"");
            sb.Append(textFormName);
            sb.Append("/"");
            sb.Append("/r/n");
            sb.Append("/r/n");
            sb.Append(strText);
            sb.Append("/r/n");

            // Return te post message header in a byte array encoded as UTF8.
            return Encoding.UTF8.GetBytes(sb.ToString());
        }
        protected byte[] CreateImageFile(string boundary, string fileFormName, string filePath)
        {
            // Build up the post message header.
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(boundary);
            sb.Append("/r/n");
            sb.Append("Content-Disposition: form-data; name=/"");
            sb.Append(fileFormName);
            sb.Append("/"; filename=/"");
            sb.Append(Path.GetFileName(filePath));
            sb.Append("/"");
            sb.Append("/r/n");
            sb.Append("Content-Type: ");
            sb.Append("image/pjpeg");
            sb.Append("/r/n");
            sb.Append("/r/n");

            // Return te post message header in a byte array encoded as UTF8.
            return Encoding.UTF8.GetBytes(sb.ToString());
        }

        private void SendMMSMessage(String strMobile, String strSubject, String strText, String strImagePath)
        {
            // Create a FileInfo object of the file.
            FileInfo fileInfo = new FileInfo(strImagePath);

            Uri myUri = new Uri("http://127.0.0.1:8800/Send%20MMS%20Message.htm");

            // Instantiate a new WebRequest.
            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(myUri);
            webrequest.Method = "POST";
            webrequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
            webrequest.Referer = "http://127.0.0.1:8800/Send%20MMS%20Message.htm";
            webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
            webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)";
            webrequest.KeepAlive = true;

            // Create the post message header.
            // Create PhoneNumber
            byte[] postPhoneNumber = CreateTextData(boundary, "PhoneNumber", strMobile);

            // Create MMSFrom
            byte[] postMMSFrom = CreateTextData(boundary, "MMSFrom", "");

            // Create MMSSubject
            byte[] postMMSSubject = CreateTextData(boundary, "MMSSubject", strSubject);

            // Create MMSText
            byte[] postMMSText = CreateTextData(boundary, "MMSText", strText);

            // Create MMSFile
            byte[] postImageBytes = CreateImageFile(boundary, "MMSFile", strImagePath);

            //Create Submit
            byte[] postSubmitBytes = CreateImageFile(boundary, "Submit", "Submit");

            // Build the trailing boundary string as a byte array
            // ensuring the boundary appears on a line by itself
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("/r/n--" + boundary + "/r/n");

            // Create the Stream objects.
            FileStream fileStream = null;
            Stream requestStream = null;

            try
            {
                // Instantiate the local FileStream.
                fileStream = new FileStream(strImagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                long length = postPhoneNumber.Length + postMMSFrom.Length + postMMSSubject.Length + postMMSText.Length
                    + postImageBytes.Length + fileStream.Length + postSubmitBytes.Length + boundaryBytes.Length;

                webrequest.ContentLength = length;

                // Get the request stream from the webrequest.
                requestStream = webrequest.GetRequestStream();

                // Write out our post header
                requestStream.Write(postPhoneNumber, 0, postPhoneNumber.Length);
                requestStream.Write(postMMSFrom, 0, postMMSFrom.Length);
                requestStream.Write(postMMSSubject, 0, postMMSSubject.Length);
                requestStream.Write(postMMSText, 0, postMMSText.Length);
                requestStream.Write(postImageBytes, 0, postImageBytes.Length);

                // Write out the file contents
                byte[] buffer = new Byte[fileStream.Length];
                int bytesRead = 0;
                long totalBytes = 0;

                // Read the bytes from the local file stream.
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    // Write the readed bytes to the request stream.
                    requestStream.Write(buffer, 0, bytesRead);
                    totalBytes += bytesRead;
                }

                // Write submit
                requestStream.Write(postSubmitBytes, 0, postSubmitBytes.Length);

                // Write out the trailing boundary
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

                // Get response.
                using (WebResponse response = webrequest.GetResponse())
                {
                    Stream responseStream = null;

                    try
                    {
                        // Get response stream.
                        responseStream = response.GetResponseStream();

                        StreamReader streamRead = new StreamReader(responseStream);

                        streamRead.Close();

                    }
                    finally
                    {
                        // Close response stream when it exists.
                        if (responseStream != null)
                        {
                            responseStream.Close();
                        }

                        // Close the response.
                        response.Close();
                    }
                }
            }
            finally
            {
                // Cleanup uses.
                if (requestStream != null)
                {
                    try
                    {
                        requestStream.Close();
                    }
                    catch
                    {
                        // Eat up exception.
                    }
                }

                if (fileStream != null)
                {
                    try
                    {
                        fileStream.Close();
                    }
                    catch
                    {
                        // Eat up exception.
                    }
                }

                if (webrequest != null)
                {
                    try
                    {
                        // Abort webrequest.
                        webrequest.Abort();
                    }
                    catch (WebException caught)
                    {
                        // Eat up exception and close
                        // response then exists.
                        if (caught.Response != null)
                        {
                            caught.Response.Close();
                        }
                    }
                }
            }
        }

这里只是一些随写,更具体的信息可以参考NowSMS的网站,和GPRS Modem的厂商站点和手册.

如果Modem有问题,或者想知道Modem是否支持SMS和MMS,可以使用HyperTerminal连接Modem测试,有关GPRS Modem的一些Command可以在网络上搜到.
AT+CREG? - if registered on network second value should be 1
AT+COPS? - returns Network operator eg. Cingular
AT+CSQ - checks signal strength, value should be greater than 10 

你可能感兴趣的:(使用NowSMS Gateway来接收发送短信和彩信)