发送短信工具类(亿美短信平台接口)

时间:2022-09-16 08:58:06

一般我们都是需要接入第三方短信平台,我这边是接入亿美的短信平台sdk,在我们的项目中只需要导入emayclient.jar就可以使用发送接口,当然前提是你有账户和密码,就涉及到购买了。不过其他不多讲,我这边只传达怎么调用,以及分享一下调用的工具类。

SmsUtil的代码如下:

package com.yiyong.mavenspring.demo.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.emay.sdk.client.api.Client;

/**
* @author yiyong wu
* @date 2016年1月11日 下午1:54:30
*/
public class SendSmsUtil {

protected static Logger log = LoggerFactory.getLogger(SendSmsUtil.class);

private static String smsSerial = "你的账户";

private static String smsKey = "你对应的密码";

private static Boolean smsAllow = true;

private static Client client;

static {
try {
client = new Client(smsSerial, smsKey);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 发送短信
*
* @param mobiles
* 手机号码数组
* @param content
* 短信内容
* @return
*/
public static int sendSMS(String[] mobiles, String content) {

// 允许发送配置为true时,才能够发出短信
if (!smsAllow) {
return 403;
}
int res = 404;
try {
res = client.sendSMS(mobiles, content, 3);
System.out.println("==================" + res);
log.info("Send SMS: mobiles = " + mobiles + ", content = "
+ content + ", res = " + res);
return res;
} catch (Exception e) {
log.error("send sms failed: mobiles = " + mobiles + ", content = "
+ content + "res = " + res);
return 404;
}
}



public static void main(String[] args) {
String[] s = { "***********" };
System.out.println("sdfsd 回复TD退订");//有时候一些服务会必须要求添加回复td退订
System.out.println(SendSmsUtil.sendSMS(s, "你要发送的内容"));//一般发送成功会返回0
}

}



在你的控制层中调用一般会这样掉用,这边我只分析一个路径访问的方法,不具体分享整个类(因为这边我是用ajax访问执行,需要在前端确认总共发送了多少条,而且我的号码也是根据我那个CVS文件读取工具读取的,也可以是txt文件读取,所以要是看不懂我这边的调用,可以看一下前面那个excel文件读取工具类)

/**
* add by yiyong wu 201601114(后端短信发送)
* @param request
* @param response
* @return
*/
@ResponseBody
@RequestMapping(value="sendmessage/send",method = RequestMethod.POST)
public Map<String,String> sendMessages(HttpServletRequest request,HttpServletResponse response){
Map<String,String> map = new HashMap<String,String>();
String messageContent = request.getParameter("messageContent");
String[] phoneNums = UploadPhoneNumUtil.uploadPhoneNum(request);

List<String> list =new ArrayList(Arrays.asList(phoneNums));

int size = list.size();
List<String> small = Lists.newArrayList();
int flag = 0;
log.info("需要发送" + size + "条短信------开始发送!");
for (int i = 0; i < size; i++) {
small.add(list.get(i));
if (small.size() == 200) {
String[] sp = (String[]) small
.toArray(new String[small.size()]);
log.info("此次一共要发送" + small.size() + "条短信!");
log.info("此次发送手机号码为:" + small);
SendSmsUtil.sendSMS(sp, messageContent);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error("睡眠失败!", e);
}
small.clear();
}
flag = small.size();
}
if (flag != 200) {
String[] sq = (String[]) small.toArray(new String[small.size()]);
log.info("此次一共要发送" + small.size() + "条短信!");
log.info("此次发送手机号码为:" + small);

SendSmsUtil.sendSMS(sq, messageContent);
}
log.info("需要发送" + size + "条短信------发送完毕!");

map.put("sended", "success");
map.put("sendNum", phoneNums.length+"");
return map;
}