网站发送短信的工具类,验证码、通知消息等

import java.net.URLEncoder;
import java.util.Random;

import com.bcloud.msg.http.HttpSender;//还需要一个http.jar包

public class SmsUtil{

private static final String URL_SendSM = "http://ip/msg/HttpSendSM";          //应用地址+单独发送方法
private static final String URL_BatchSendS = "http://ip/msg/HttpBatchSendS";  //应用地址+单独群发方法
private static final String ACCOUNT = "******";//账号
private static final String PSWD = "*******";//密码
private static final String SIGNATURE = "【我的技术总结】";
private static final String CONTENT_END = "退订回N";

private boolean needstatus = true;//是否需要状态报告,需要true,不需要false
private String Phone,Content;

private String Product = "";//产品ID
private String Extno = "012";//扩展码

private int Type = 1;//1,代表给一个手机号发短信,2,代表给多个人发短信

public SmsUtil(String phone,int type){

    super();
    this.Phone = phone;
    this.Type = type;

}

public void setProduct(String product){
    this.Product = product;
}
public void setExtno(String extno){
    this.Extno = extno;
}
public String getProduct(){
    return this.Product;
}
public String getExtno(){
    return this.Extno;
}
public void setContent(String content){
    this.Content = content;
}
public String getContent(){
    return SIGNATURE+this.Content+CONTENT_END;
}
public void setNeedstatus(boolean flag){
    this.needstatus = flag;
}

public Boolean getNeedstatus(){
    return this.needstatus;
}

public String Send(){
    String returnFlag = "";
    if(Type == 1){
        returnFlag +=Sendsms();
    }else {
        returnFlag +=batchSendsms();
    }
    return returnFlag;
}
//只给一个手机号码发送短信
public String  Sendsms(){
    String returnString = "";
    try {
        returnString = HttpSender.send(URL_SendSM, ACCOUNT, PSWD, Phone, SIGNATURE+Content+CONTENT_END, needstatus, Product, Extno);

// System.out.println(returnString);
return returnString;
//TODO 处理返回值,参见HTTP协议文档
} catch (Exception e) {
//TODO 处理异常
return returnString;
}

}
//给多个号码发送短信,用“,”隔开
public String  batchSendsms(){
    String returnString = "";
    try {
        returnString = HttpSender.batchSend(URL_BatchSendS, ACCOUNT, PSWD, Phone, SIGNATURE+Content+CONTENT_END, needstatus, Product, Extno);

// System.out.println(returnString);
return returnString;
//TODO 处理返回值,参见HTTP协议文档
} catch (Exception e) {
//TODO 处理异常
return returnString;
}
}
//得到随机数
public String getRandNum(int charCount) {

    String charValue = "";
    for (int i = 0; i < charCount; i++) {
        char c = (char) (randomInt(0, 10) + '0');
        charValue += String.valueOf(c);
    }
    return charValue;
}
public  int randomInt(int from, int to) {
    Random r = new Random();
    return from + r.nextInt(to - from);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值