JAVAのSMSメール送信機能モジュールの開発

8102 ワード

JAvaは中国ネット建設が提供するSMSプラットフォームを利用してメールの送信を完了する
まずアカウント転送ゲートを登録して左側の機能欄のメールAPIインタフェースをクリックして自分のインタフェースの鍵を設定し、次のコードを実行すればOKです.

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class SendMsg_webchinese {

    public static void main(String[] args) throws HttpException, IOException {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
        post.addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded;charset=gbk");
        NameValuePair[] data = { new NameValuePair("Uid", "     "),
                new NameValuePair("Key", "api  "),
                new NameValuePair("smsMob", "   "),
                new NameValuePair("smsText", "          ,        ?") };//     
        post.setRequestBody(data);

        client.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        System.out.println("statusCode:" + statusCode);
        for (Header h : headers) {
            System.out.println("---" + h.toString());
        }
        String result = new String(post.getResponseBodyAsString().getBytes(
                "gbk"));
        System.out.println(result);

    }

}

このプログラムでは、次の3つのjarパッケージcommons-logging-1.1.1.jar commons-httpclient-3.1.jar commons-codec-1.4.jarを使用する必要があります.
GBKコード送信インタフェースアドレス:http://gbk.sms.webchinese.cn/?Uid=当駅ユーザー名&Key=インタフェースセキュリティパスワード&smsMob=携帯電話番号&smsText=メール内容UTF-8コード送信インタフェースアドレス:http://utf8.sms.webchinese.cn/?Uid=当駅ユーザ名&Key=インタフェースセキュリティパスワード&smsMob=携帯電話番号&smsText=メール内容取得メール数インタフェースアドレス(UTF 8): http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&UID=当駅ユーザー名&Key=インタフェースセキュリティ情報取得メール数インタフェースアドレス(GBK):http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&UID=当駅ユーザ名&Key=インタフェースセキュリティパスワード
ヒント:HTTPがURLインタフェースを呼び出す場合、パラメータ値はURL符号化してから呼び出さなければならない
パラメータ変数説明Gbk符号化Urlhttp://gbk.sms.webchinese.cn/ Utf-8符号化Urlhttp://utf8.sms.webchinese.cn/ UID当駅ユーザー名(当駅ユーザー名がない場合は先に登録してください)Key登録時に記入したインタフェースセキュリティパスワード(ユーザープラットフォームでセキュリティパスワードを変更できます)smsMobの携帯電話番号(複数の携帯電話番号は半角カンマで区切ってください)smsTextメールの内容は、最大300字、普通メール70字/条、長短手紙64字/条料金計算複数携帯電話番号を半角で区切ってください.例えば、1388888888888613888888888888888713888888888888一度に最大50携帯電話にメールの内容を送信することは長短手紙、最大300字、普通メール70字/条、長短手紙64字/条料金計算
メール送信後の戻り値は、-1が当該ユーザアカウント-2鍵を持っていないことを示す(ユーザパスワードではない)-3ショートメッセージの数が足りない-11このユーザーが無効になった-14ショートメッセージの内容に不正な文字が現れた-4携帯電話番号のフォーマットが正しくない-41携帯電話番号が空-42ショートメッセージの内容が空が0より大きい以下は異なる言語でSMSインタフェースを呼び出す簡単なdemoである:
  • ASP呼び出し例「共通関数」「urlターゲットページアドレスを入力し、戻り値getHTTPPageはターゲットページのhtmlコードfunction getHTTPPage(url)dim Http set Http=server.createobject(「MSXML 2.XMXMLTTP」)Http.open「GET」、url、false Http.send()if Http.readystate<>4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,”GB2312”) set http=nothing if err.number<>0 then err.Clear end function Function BytesToBstr(body,Cset) dim objstream set objstream = Server.CreateObject(“adodb.stream”) objstream.Type = 1 objstream.Mode =3 objstream.Open objstream.Write body objstream.Position = 0 objstream.Type = 2 objstream.Charset = Cset BytesToBstr = objstream.ReadText objstream.Close set objstream = nothing End Function

  • 『自分で提出したURLを組み合わせて自分のアカウントとパスワードsms_url=』に加入するhttp://sms.webchinese.cn/web_api/?Uid=アカウント&Key=インタフェースキー&smsMob=携帯電話番号&smsText=メール内容"response.write getHTTPPage(sms_url)%>
    2.C#コール
    //必要なネーミングスペースusing System.Net;using System.IO;using System.Text;//呼び出すときは綴ったURLをこの関数に渡すだけ.戻り値を判断すればpublic string GetHtmlFromUrl(string url){string string strRet=null;
    if(url==null || url.Trim().ToString()==”“) { return strRet; } string targeturl = url.Trim().ToString(); try { HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl); hr.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)”; hr.Method = “GET”; hr.Timeout = 30 * 60 * 1000; WebResponse hs = hr.GetResponse(); Stream sr = hs.GetResponseStream(); StreamReader ser = new StreamReader(sr, Encoding.Default); strRet = ser.ReadToEnd(); } catch (Exception ex) { strRet = null; } return strRet; }
    3.JAVA呼び出し
    import java.io.UnsupportedEncodingException; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod;
    public class SendMsg_webchinese {
    public static void main(String[] args)throws Exception {
    HttpClient client = new HttpClient(); PostMethod post = new PostMethod(“http://gbk.sms.webchinese.cn");post.addRequestHeader("Content-Type","アプリケーション/x-www-form-urlencoded;charset=gbk");//ヘッダファイルにトランスコードNameValue Pair[]data={new NameValue Pair("UID","当駅ユーザ名"),new NameValue Pair("Key",「インタフェースセキュリティパスワード」)、new NameValuePair(「smsMob」、「携帯電話番号」)、new NameValuePair(「smsText」、「メール内容」)};post.setRequestBody(data);
    client.executeMethod(post); Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println(“statusCode:”+statusCode); for(Header h : headers) { System.out.println(h.toString()); } String result = new String(post.getResponseBodyAsString().getBytes(“gbk”)); System.out.println(result);
    post.releaseConnection();
    }
    }
    jarパッケージcommons-logging-1.1.1.jar commons-httpclient-3.1.jar commons-codec-1.4.jar
    4.PHP
    $url=’http://sms.webchinese.cn/web_api/?Uid=アカウント&Key=インタフェースキー&smsMob=携帯電話番号&smsText=メール内容;
    echo Get( url);functionGet( url) { if(function_exists(‘file_get_contents’)) { filecontents=filegetcontents( url); } else { ch=curlinit(); timeout = 5; curl_setopt ( ch,CURLOPTURL, url); curl_setopt ( ch,CURLOPTRETURNTRANSFER,1);curlsetopt( ch, CURLOPT_CONNECTTIMEOUT, timeout); file_contents = curl_exec( ch);curlclose( ch); } return $file_contents; }
    5.VB.NET‘呼び出しメール,NoList受信番号.複数の間で使用,分割,Memoコンテンツ70字Public Function SendSMS(ByVal NoList As String,ByVal Memo As String)As String Dim Url As String="http://sms.webchinese.cn/web_api/?Uid=アカウント&Key=インタフェースキー&smsMob=携帯電話番号&smsText=メール内容」Dim webClient As New Net.WebClient() Try ‘Dim responseData As Byte() = Dim srcString As String = webClient.DownloadString(Url) Return srcString Catch Return “-444” End Try End Function
    オリジナルライターに感謝します