一般我们都是需要接入第三方短信平台,我这边是接入亿美的短信平台sdk,在我们的项目中只需要导入emayclient.jar就可以使用发送接口,当然前提是你有账户和密码,就涉及到购买了。不过其他不多讲,我这边只传达怎么调用,以及分享一下调用的工具类。
SmsUtil的代码如下:
1.package com.yiyong.mavenspring.demo.util;
2.
3.import org.slf4j.Logger;
4.import org.slf4j.LoggerFactory;
5.
6.import cn.emay.sdk.client.api.Client;
7.
8./**
9. * @author yiyong wu
10. * @date 2016年1月11日 下午1:54:30
11. */
12.public class SendSmsUtil {
13.
14. protected static Logger log = LoggerFactory.getLogger(SendSmsUtil.class);
15.
16. private static String smsSerial = "你的账户";
17.
18. private static String smsKey = "你对应的密码";
19.
20. private static Boolean smsAllow = true;
21.
22. private static Client client;
23.
24. static {
25. try {
26. client = new Client(smsSerial, smsKey);
27. } catch (Exception e) {
28. e.printStackTrace();
29. }
30. }
31.
32. /**
33. * 发送短信
34. *
35. * @param mobiles
36. * 手机号码数组
37. * @param content
38. * 短信内容
39. * @return
40. */
41. public static int sendSMS(String[] mobiles, String content) {
42.
43. // 允许发送配置为true时,才能够发出短信
44. if (!smsAllow) {
45. return 403;
46. }
47. int res = 404;
48. try {
49. res = client.sendSMS(mobiles, content, 3);
50. System.out.println("==================" + res);
51. log.info("Send SMS: mobiles = " + mobiles + ", content = "
52. + content + ", res = " + res);
53. return res;
54. } catch (Exception e) {
55. log.error("send sms failed: mobiles = " + mobiles + ", content = "
56. + content + "res = " + res);
57. return 404;
58. }
59. }
62.
63. public static void main(String[] args) {
64. String[] s = { "***********" };
65. System.out.println("sdfsd 回复TD退订");//有时候一些服务会必须要求添加回复td退订
66. System.out.println(SendSmsUtil.sendSMS(s, "你要发送的内容"));//一般发送成功会返回0
67. }
68.
69.}
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文件读取工具类)
1./**
2. * add by yiyong wu 201601114(后端短信发送)
3. * @param request
4. * @param response
5. * @return
6. */
7. @ResponseBody
8. @RequestMapping(value="sendmessage/send",method = RequestMethod.POST)
9. public Map<String,String> sendMessages(HttpServletRequest request,HttpServletResponse response){
10. Map<String,String> map = new HashMap<String,String>();
11. String messageContent = request.getParameter("messageContent");
12. String[] phoneNums = UploadPhoneNumUtil.uploadPhoneNum(request);
13.
14. List<String> list =new ArrayList(Arrays.asList(phoneNums));
15.
16. int size = list.size();
17. List<String> small = Lists.newArrayList();
18. int flag = 0;
19. log.info("需要发送" + size + "条短信------开始发送!");
20. for (int i = 0; i < size; i++) {
21. small.add(list.get(i));
22. if (small.size() == 200) {
23. String[] sp = (String[]) small
24. .toArray(new String[small.size()]);
25. log.info("此次一共要发送" + small.size() + "条短信!");
26. log.info("此次发送手机号码为:" + small);
27. SendSmsUtil.sendSMS(sp, messageContent);
28. try {
29. Thread.sleep(1000);
30. } catch (InterruptedException e) {
31. log.error("睡眠失败!", e);
32. }
33. small.clear();
34. }
35. flag = small.size();
36. }
37. if (flag != 200) {
38. String[] sq = (String[]) small.toArray(new String[small.size()]);
39. log.info("此次一共要发送" + small.size() + "条短信!");
40. log.info("此次发送手机号码为:" + small);
41.
42. SendSmsUtil.sendSMS(sq, messageContent);
43. }
44. log.info("需要发送" + size + "条短信------发送完毕!");
45.
46. map.put("sended", "success");
47. map.put("sendNum", phoneNums.length+"");
48. return map;
49. }
/**
* 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;
}
一般我们都是需要接入第三方短信平台,我这边是接入亿美的短信平台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
- }
- }
- /**
- * 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;
- }
-