使用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 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
您好,要在 Android 中发送彩信并监听短信,可以按照以下步骤操作: 1.发送彩信 发送彩信需要使用 MMS(Multimedia Messaging Service)协议,具体步骤如下: ```java // 创建一个 MMS 编辑器 Uri uri = Uri.parse("content://mms"); Intent intent = new Intent(Intent.ACTION_SEND, uri); intent.putExtra("sms_body", "这是彩信的正文"); intent.putExtra("address", "1234567890"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/test.jpg"))); intent.setType("image/jpeg"); startActivity(intent); ``` 这个 Intent 会启动系统自带的彩信编辑器,用户可以在编辑器中编辑彩信的内容,包括添加图片、声音等元素。用户编辑完毕后,可以选择发送彩信,系统会将彩信发送给指定的号码。 2.监听彩信 要监听彩信,可以注册一个 ContentObserver,监听 MMS 的变化。具体步骤如下: ```java // 创建一个 ContentObserver ContentObserver observer = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); // 处理彩信变化 } }; // 注册 ContentObserver getContentResolver().registerContentObserver(Uri.parse("content://mms"), true, observer); ``` 这样,当有新的彩信到来时,onChange() 方法就会被调用,我们可以在其中处理彩信的内容。 3.监听短信 要监听短信,可以注册一个 BroadcastReceiver,监听短信的广播。具体步骤如下: ```java // 创建一个 BroadcastReceiver BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // 处理短信 } }; // 注册 BroadcastReceiver IntentFilter filter = new IntentFilter(); filter.addAction("android.provider.Telephony.SMS_RECEIVED"); registerReceiver(receiver, filter); ``` 这样,当有新的短信到来时,onReceive() 方法就会被调用,我们可以在其中处理短信的内容。 希望能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值