SendMsgUtil.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.kmall.api.util;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. /**
  7. * @author huangyaqin
  8. * @version 1.0
  9. * 2018-10-16 11:27
  10. */
  11. public class SendMsgUtil {
  12. /**
  13. * 发送短信消息
  14. * 方法说明
  15. * @Discription:扩展说明
  16. * @param phones
  17. * @param content
  18. * @return
  19. * @return String
  20. */
  21. @SuppressWarnings("deprecation")
  22. public static String sendMsg(String phones,String content) {
  23. try {
  24. content = java.net.URLEncoder.encode(content,"utf-8");
  25. System.out.println(content);
  26. } catch (UnsupportedEncodingException e) {
  27. e.printStackTrace();
  28. }
  29. String sendMsgapiKey = "d6d33c8e7ad847cc3f3d7e9f75d0abb1";
  30. //短信接口URL提交地址
  31. String url = "https://api.dingdongcloud.com/v1/sms/sendyzm";
  32. Map<String, String> params = new HashMap<String, String>();
  33. params.put("name", "13530612313");
  34. params.put("pwd", "nm81875o");
  35. // params.put("dxlbid", "短信类别编号");
  36. // params.put("extno", "扩展编号");
  37. params.put("apikey", sendMsgapiKey);
  38. //手机号码,多个号码使用英文逗号进行分割
  39. params.put("mobile", phones);
  40. //将短信内容进行URLEncoder编码
  41. params.put("content", URLEncoder.encode(content));
  42. params.put("sendTs", "");
  43. return HttpRequestUtil.postRequest(url, params);
  44. }
  45. /**
  46. * 随机生成6位随机验证码
  47. * 方法说明
  48. * @Discription:扩展说明
  49. * @return
  50. * @return String
  51. */
  52. public static String createRandomVcode(){
  53. //验证码
  54. String vcode = "";
  55. for (int i = 0; i < 6; i++) {
  56. vcode = vcode + (int)(Math.random() * 9);
  57. }
  58. return vcode;
  59. }
  60. /**
  61. * 测试
  62. * 方法说明
  63. * @Discription:扩展说明
  64. * @param args
  65. * @return void
  66. */
  67. public static void main(String[] args) {
  68. // System.out.println(SendMsgUtil.createRandomVcode());
  69. // System.out.println("&ecb=12".substring(1));
  70. System.out.println(sendMsg("18201150549", "【签名】尊敬的用户,您的验证码为" + SendMsgUtil.createRandomVcode() + ",请在10分钟内输入。请勿告诉其他人!"));
  71. }
  72. }