Java 通过SMS短信平台实现发短信功能

短信工具类

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class SMSSender {
	private static SMSSender ourInstance = new SMSSender();

	public static SMSSender getInstance() {
		return ourInstance;
	}

	private Gson gson = new Gson();

	private static final String prefix = "【平台APP】";
	private static final Logger logger = LogManager.getLogger(SMSSender.class);
          //发送短信的地址
	private static final String url = "http://IP地址:端口号/sms.aspx?action=send&userid=短信用户id&account=账号&password=密码&mobile=%s&content=%s";


	public boolean sendValidation(String mobile, String content) {
		if (!mobile.matches("1\\d{10}")) {
			logger.error("短信发送失败!不合法的手机号:" + mobile);
			return false;
		}
		try {
			return send(mobile, URLEncoder.encode("【平台APP】" + content, "utf-8"));

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
	}

	private boolean send(String mobile, String content) {
		try {
			Response bean = Request.Post(String.format(url, new Object[]{mobile, content})).addHeader("Accept", "text/xml").addHeader("Content-Type", "text/xml;charset=utf-8").execute();
			String result = bean.returnContent().asString();
			return result.contains("<returnstatus>Success</returnstatus>");
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}


		class Result {
			private int code;
			private int fee;
			private String mobile;
			private String msg;

			Result() {
			}

			public int getCode() {
				return code;
			}

			public void setCode(int code) {
				this.code = code;
			}

			public int getFee() {
				return fee;
			}

			public void setFee(int fee) {
				this.fee = fee;
			}

			public String getMobile() {
				return mobile;
			}

			public void setMobile(String mobile) {
				this.mobile = mobile;
			}

			public String getMsg() {
				return msg;
			}


			public void setMsg(String msg) {
				this.msg = msg;
			}
		}

		class Wrapper {
			private List<Result> data;
			@SerializedName("total_fee")
			private int total;

			Wrapper() {
			}

			public List<Result> getData() {
				return data;
			}

			public void setData(List<Result> data) {
				this.data = data;
			}

			public int getTotal() {
				return total;
			}

			public void setTotal(int total) {
				this.total = total;
			}
		}
	}
}

使用方法:

SMSSender.getInstance().sendValidation("电话号码", "短信内容");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值