java邮件发送和短信发送(二)

[java]  view plain copy
  1. /** 
  2.  * 功能: 短信发送服务 
  3.  * <p> 
  4.  * 用法: 
  5.  *  
  6.  * @version 1.0 
  7.  */  
  8.   
  9. public class SmsSenderImpl implements SmsSender,InitializingBean {  
  10.     /** 
  11.      * Logger for this class 
  12.      */  
  13.     private static final Logger logger = Logger.getLogger(SmsSenderImpl.class);  
  14.   
  15.     @Autowired  
  16.     private ConfigService configService;  
  17.       
  18.     private String smsUrl;  
  19.     private String cpidName;  
  20.     private String cpidValue;  
  21.     private String pwdName;  
  22.     private String pwdValue;  
  23.     private String pidName;  
  24.     private String pidValue;  
  25.     private String phoneName;  
  26.     private String msgName;  
  27.     private int maxLength = 60// 默认值   
  28.       
  29.     @Override  
  30.     public void sendSms(String mobilePhone, String message) throws CheckException {  
  31.         send(message, mobilePhone);  
  32.           
  33.     }  
  34.   
  35.     @Override  
  36.     public void sendSms(String[] mobilePhones, String message) throws CheckException {  
  37.         if (ArrayUtils.isEmpty(mobilePhones)){  
  38.             throw new CheckException("手机号码不能为空");  
  39.         }  
  40.         for (String phone : mobilePhones) {  
  41.             sendSms(phone, message);  
  42.         }  
  43.           
  44.     }  
  45.       
  46.   
  47.     /** 
  48.      * 如果超过短信的长度,则分成几条发 
  49.      * @param content 
  50.      * @param phoneNo 
  51.      * @return 
  52.      * @throws CheckException 
  53.      */  
  54.     private String send(String content,String phoneNo) throws CheckException{  
  55.         content = StringUtils.trimToEmpty(content);  
  56.         phoneNo = StringUtils.trimToEmpty(phoneNo);  
  57.           
  58.         if (StringUtils.isEmpty(content)){  
  59.             throw new CheckException("短信内容为空");  
  60.         }  
  61.         if (StringUtils.isEmpty(phoneNo)){  
  62.             throw new CheckException("手机号为空");  
  63.         }  
  64.         // 如果服务未准备好,先初始化   
  65.         if (!isReady()) {  
  66.             try {  
  67.                 init();  
  68.                 // 初始化后,服务仍未准备好   
  69.                 if (!isReady()) {  
  70.                     throw new CheckException("邮件服务初始化异常");  
  71.                 }  
  72.             } catch (Exception e) {  
  73.                 logger.error("send(String, String)", e);  
  74.                   
  75.                 throw new CheckException("邮件服务初始化异常");  
  76.             }  
  77.         }  
  78.           
  79.         // 如果超过最大长度,则分成几条发送   
  80.         int count = content.length() / maxLength;  
  81.         int reminder = content.length() % maxLength;  
  82.            
  83.         if (reminder != 0 ){  
  84.            count += 1;  
  85.         }  
  86.         StringBuffer result = new StringBuffer();  
  87.         int i = 0;  
  88.         while (count > i){  
  89.            result.append(doSend(StringUtils.substring(content, i*maxLength, (i+1)*maxLength),phoneNo));  
  90.            result.append(";");  
  91.            i ++;  
  92.         }  
  93.         return result.toString();  
  94.     }  
  95.       
  96.     private boolean isReady(){  
  97.         return !(smsUrl == null || cpidName == null || cpidValue == null  
  98.                 || pwdName == null || pwdValue == null || pidName == null  
  99.                 || pidValue == null || phoneName == null || msgName == null || maxLength <= 0);  
  100.     }  
  101.     /** 
  102.      * @param content 
  103.      * @param phoneNo 
  104.      * @return 
  105.      * @throws CheckException 
  106.      */  
  107.     private String doSend(String content,String phoneNo) throws CheckException{  
  108.           
  109.         // 使用httpclient模拟http请求   
  110.         HttpClient client = new HttpClient();  
  111.         // 设置参数编码   
  112.         client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");  
  113.           
  114.         PostMethod method = new PostMethod(smsUrl);  
  115.           
  116.         method.addParameter(cpidName, cpidValue);  
  117.         method.addParameter(pidName, pidValue);  
  118.         method.addParameter(pwdName, pwdValue);  
  119.         method.addParameter(phoneName, phoneNo);  
  120.         method.addParameter(msgName, content);  
  121.           
  122.           
  123.         BufferedReader br = null;  
  124.         String reponse  = null;  
  125.         try {  
  126.             int returnCode = client.executeMethod(method);  
  127.   
  128.             if (returnCode != HttpStatus.SC_OK) {  
  129.                 // 请求出错   
  130.                 throw new CheckException("短信接口异常");  
  131.   
  132.             }  
  133.             br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));  
  134.             reponse = br.readLine();  
  135.             String responseCode = StringUtils.substring(reponse, 01);  
  136.             if (!"0".equals(responseCode)){  
  137.                 throw new CheckException(getResponseMsg(responseCode));  
  138.             }  
  139.   
  140.         } catch (Exception e) {  
  141.             logger.error("doSend(String, String)", e);  
  142.   
  143.             if (e instanceof CheckException){  
  144.                 throw (CheckException)e;  
  145.             }else{  
  146.                 throw new CheckException("未知异常"); // 未知异常   
  147.             }  
  148.         } finally {  
  149.             method.releaseConnection();  
  150.             if (br != null)  
  151.                 try {  
  152.                     br.close();  
  153.                 } catch (Exception e1) {  
  154.                     logger.error("doSend(String, String)", e1);  
  155.   
  156.                     e1.printStackTrace();  
  157.                 }  
  158.         }  
  159.   
  160.         return reponse;  
  161.     }  
  162.   
  163.     public void afterPropertiesSet() throws Exception {  
  164.         // 初始化   
  165.         init();  
  166.     }  
  167.       
  168.     private void init() throws Exception{  
  169.           
  170.         smsUrl = configService.getConfig(BasePropertyID.SMS_URL_ID);  
  171.         cpidName = configService.getConfig(BasePropertyID.SMS_CPID_NAME_ID);  
  172.         cpidValue = configService.getConfig(BasePropertyID.SMS_CPID_VALUE_ID);  
  173.         pwdName = configService.getConfig(BasePropertyID.SMS_PWD_NAME_ID);  
  174.         pwdValue = configService.getConfig(BasePropertyID.SMS_PWD_VALUE_ID);  
  175.         pidName = configService.getConfig(BasePropertyID.SMS_PID_NAME_ID);  
  176.         pidValue = configService.getConfig(BasePropertyID.SMS_PID_VALUE_ID);  
  177.         phoneName = configService.getConfig(BasePropertyID.SMS_PHONE_NAME_ID);  
  178.         msgName = configService.getConfig(BasePropertyID.SMS_MSG_NAME_ID);  
  179.         maxLength = configService.getConfigByInteger(BasePropertyID.SMS_MSG_MAXLENGTH_ID);  
  180.           
  181.     }  
  182.       
  183.     private String getResponseMsg(String code){  
  184.         String msg = "未知返回值:" + code;  
  185.         if ("1".equals(code)) {  
  186.             msg = "手机号码非法";  
  187.         } else if ("2".equals(code)) {  
  188.             msg = "用户存在于黑名单列表";  
  189.         } else if ("3".equals(code)) {  
  190.             msg = "接入用户名或密码错误";  
  191.         } else if ("4".equals(code)) {  
  192.             msg = "产品代码不存在";  
  193.         } else if ("5".equals(code)) {  
  194.             msg = "IP非法";  
  195.         } else if ("6".equals(code)) {  
  196.             msg = "源号码错误";  
  197.         } else if ("7".equals(code)) {  
  198.             msg = "调用网关错误";  
  199.         } else if ("8".equals(code)) {  
  200.             msg = "消息长度超过60";  
  201.         } else if ("-1".equals(code)) {  
  202.             msg = "短信内容为空";  
  203.         } else if ("-2".equals(code)) {  
  204.             msg = "手机号为空";  
  205.         }else if ("-3".equals(code)) {  
  206.             msg = "邮件服务初始化异常";  
  207.         }else if ("-4".equals(code)) {  
  208.             msg = "短信接口异常";  
  209.         }  
  210.         return msg;  
  211.     }  
  212.   
  213.       
  214. }  
[java]  view plain  copy
  1. /** 
  2.  * 功能: 短信发送服务 
  3.  * <p> 
  4.  * 用法: 
  5.  *  
  6.  * @version 1.0 
  7.  */  
  8.   
  9. public class SmsSenderImpl implements SmsSender,InitializingBean {  
  10.     /** 
  11.      * Logger for this class 
  12.      */  
  13.     private static final Logger logger = Logger.getLogger(SmsSenderImpl.class);  
  14.   
  15.     @Autowired  
  16.     private ConfigService configService;  
  17.       
  18.     private String smsUrl;  
  19.     private String cpidName;  
  20.     private String cpidValue;  
  21.     private String pwdName;  
  22.     private String pwdValue;  
  23.     private String pidName;  
  24.     private String pidValue;  
  25.     private String phoneName;  
  26.     private String msgName;  
  27.     private int maxLength = 60// 默认值  
  28.       
  29.     @Override  
  30.     public void sendSms(String mobilePhone, String message) throws CheckException {  
  31.         send(message, mobilePhone);  
  32.           
  33.     }  
  34.   
  35.     @Override  
  36.     public void sendSms(String[] mobilePhones, String message) throws CheckException {  
  37.         if (ArrayUtils.isEmpty(mobilePhones)){  
  38.             throw new CheckException("手机号码不能为空");  
  39.         }  
  40.         for (String phone : mobilePhones) {  
  41.             sendSms(phone, message);  
  42.         }  
  43.           
  44.     }  
  45.       
  46.   
  47.     /** 
  48.      * 如果超过短信的长度,则分成几条发 
  49.      * @param content 
  50.      * @param phoneNo 
  51.      * @return 
  52.      * @throws CheckException 
  53.      */  
  54.     private String send(String content,String phoneNo) throws CheckException{  
  55.         content = StringUtils.trimToEmpty(content);  
  56.         phoneNo = StringUtils.trimToEmpty(phoneNo);  
  57.           
  58.         if (StringUtils.isEmpty(content)){  
  59.             throw new CheckException("短信内容为空");  
  60.         }  
  61.         if (StringUtils.isEmpty(phoneNo)){  
  62.             throw new CheckException("手机号为空");  
  63.         }  
  64.         // 如果服务未准备好,先初始化  
  65.         if (!isReady()) {  
  66.             try {  
  67.                 init();  
  68.                 // 初始化后,服务仍未准备好  
  69.                 if (!isReady()) {  
  70.                     throw new CheckException("邮件服务初始化异常");  
  71.                 }  
  72.             } catch (Exception e) {  
  73.                 logger.error("send(String, String)", e);  
  74.                   
  75.                 throw new CheckException("邮件服务初始化异常");  
  76.             }  
  77.         }  
  78.           
  79.         // 如果超过最大长度,则分成几条发送  
  80.         int count = content.length() / maxLength;  
  81.         int reminder = content.length() % maxLength;  
  82.            
  83.         if (reminder != 0 ){  
  84.            count += 1;  
  85.         }  
  86.         StringBuffer result = new StringBuffer();  
  87.         int i = 0;  
  88.         while (count > i){  
  89.            result.append(doSend(StringUtils.substring(content, i*maxLength, (i+1)*maxLength),phoneNo));  
  90.            result.append(";");  
  91.            i ++;  
  92.         }  
  93.         return result.toString();  
  94.     }  
  95.       
  96.     private boolean isReady(){  
  97.         return !(smsUrl == null || cpidName == null || cpidValue == null  
  98.                 || pwdName == null || pwdValue == null || pidName == null  
  99.                 || pidValue == null || phoneName == null || msgName == null || maxLength <= 0);  
  100.     }  
  101.     /** 
  102.      * @param content 
  103.      * @param phoneNo 
  104.      * @return 
  105.      * @throws CheckException 
  106.      */  
  107.     private String doSend(String content,String phoneNo) throws CheckException{  
  108.           
  109.         // 使用httpclient模拟http请求  
  110.         HttpClient client = new HttpClient();  
  111.         // 设置参数编码  
  112.         client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");  
  113.           
  114.         PostMethod method = new PostMethod(smsUrl);  
  115.           
  116.         method.addParameter(cpidName, cpidValue);  
  117.         method.addParameter(pidName, pidValue);  
  118.         method.addParameter(pwdName, pwdValue);  
  119.         method.addParameter(phoneName, phoneNo);  
  120.         method.addParameter(msgName, content);  
  121.           
  122.           
  123.         BufferedReader br = null;  
  124.         String reponse  = null;  
  125.         try {  
  126.             int returnCode = client.executeMethod(method);  
  127.   
  128.             if (returnCode != HttpStatus.SC_OK) {  
  129.                 // 请求出错  
  130.                 throw new CheckException("短信接口异常");  
  131.   
  132.             }  
  133.             br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));  
  134.             reponse = br.readLine();  
  135.             String responseCode = StringUtils.substring(reponse, 01);  
  136.             if (!"0".equals(responseCode)){  
  137.                 throw new CheckException(getResponseMsg(responseCode));  
  138.             }  
  139.   
  140.         } catch (Exception e) {  
  141.             logger.error("doSend(String, String)", e);  
  142.   
  143.             if (e instanceof CheckException){  
  144.                 throw (CheckException)e;  
  145.             }else{  
  146.                 throw new CheckException("未知异常"); // 未知异常  
  147.             }  
  148.         } finally {  
  149.             method.releaseConnection();  
  150.             if (br != null)  
  151.                 try {  
  152.                     br.close();  
  153.                 } catch (Exception e1) {  
  154.                     logger.error("doSend(String, String)", e1);  
  155.   
  156.                     e1.printStackTrace();  
  157.                 }  
  158.         }  
  159.   
  160.         return reponse;  
  161.     }  
  162.   
  163.     public void afterPropertiesSet() throws Exception {  
  164.         // 初始化  
  165.         init();  
  166.     }  
  167.       
  168.     private void init() throws Exception{  
  169.           
  170.         smsUrl = configService.getConfig(BasePropertyID.SMS_URL_ID);  
  171.         cpidName = configService.getConfig(BasePropertyID.SMS_CPID_NAME_ID);  
  172.         cpidValue = configService.getConfig(BasePropertyID.SMS_CPID_VALUE_ID);  
  173.         pwdName = configService.getConfig(BasePropertyID.SMS_PWD_NAME_ID);  
  174.         pwdValue = configService.getConfig(BasePropertyID.SMS_PWD_VALUE_ID);  
  175.         pidName = configService.getConfig(BasePropertyID.SMS_PID_NAME_ID);  
  176.         pidValue = configService.getConfig(BasePropertyID.SMS_PID_VALUE_ID);  
  177.         phoneName = configService.getConfig(BasePropertyID.SMS_PHONE_NAME_ID);  
  178.         msgName = configService.getConfig(BasePropertyID.SMS_MSG_NAME_ID);  
  179.         maxLength = configService.getConfigByInteger(BasePropertyID.SMS_MSG_MAXLENGTH_ID);  
  180.           
  181.     }  
  182.       
  183.     private String getResponseMsg(String code){  
  184.         String msg = "未知返回值:" + code;  
  185.         if ("1".equals(code)) {  
  186.             msg = "手机号码非法";  
  187.         } else if ("2".equals(code)) {  
  188.             msg = "用户存在于黑名单列表";  
  189.         } else if ("3".equals(code)) {  
  190.             msg = "接入用户名或密码错误";  
  191.         } else if ("4".equals(code)) {  
  192.             msg = "产品代码不存在";  
  193.         } else if ("5".equals(code)) {  
  194.             msg = "IP非法";  
  195.         } else if ("6".equals(code)) {  
  196.             msg = "源号码错误";  
  197.         } else if ("7".equals(code)) {  
  198.             msg = "调用网关错误";  
  199.         } else if ("8".equals(code)) {  
  200.             msg = "消息长度超过60";  
  201.         } else if ("-1".equals(code)) {  
  202.             msg = "短信内容为空";  
  203.         } else if ("-2".equals(code)) {  
  204.             msg = "手机号为空";  
  205.         }else if ("-3".equals(code)) {  
  206.             msg = "邮件服务初始化异常";  
  207.         }else if ("-4".equals(code)) {  
  208.             msg = "短信接口异常";  
  209.         }  
  210.         return msg;  
  211.     }  
  212.   
  213.       
  214. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值