package org.xmx0632.deliciousfruit.utilities.sms; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xmx0632.deliciousfruit.utilities.common.CommonUtil; import com.wondertek.esmp.esms.empp.EMPPConnectResp; import com.wondertek.esmp.esms.empp.EmppApi; public class SmsSender { private static Logger log = LoggerFactory.getLogger(SmsSender.class); private String host = "192.168.0.215"; private int port = 9981; private String accountId = "555580001"; private String password = "cool1226"; private String serviceId = "555580001"; private EmppApi emppApi; private ReceiveListener receiveListener; public void setEmppApi(EmppApi emppApi) { this.emppApi = emppApi; } public void setReceiveListener(ReceiveListener listener) { this.receiveListener = listener; } public void setHost(String host) { this.host = host; } public void setPort(int port) { this.port = port; } public void setAccountId(String accountId) { this.accountId = accountId; } public void setPassword(String password) { this.password = password; } public void setServiceId(String serviceId) { this.serviceId = serviceId; } public boolean send(String message, String mobileNo) { log.debug("message:{},mobileNo:{}", message, mobileNo); if (!CommonUtil.isMobileNO(mobileNo)) { log.warn("{} is not valid mobileNo", mobileNo); return false; } try { // 建立同服务器的连接 EMPPConnectResp response = emppApi.connect(host, port, accountId, password, receiveListener); if (response == null) { log.debug("connection timeout"); return false; } log.debug(response.toString()); if (!emppApi.isConnected()) { log.debug("connection error:response status=" + response.getStatus()); return false; } } catch (Exception e) { log.error("connection error:" + e.getMessage(), e); return false; } // 发送短信 if (emppApi.isSubmitable()) { // 简单方式发送短信,支持长短信 try { int[] result = emppApi.submitMsgAsync(message, new String[] { mobileNo }, serviceId); return true; } catch (Exception e1) { log.error("send messge failed:" + e1.getMessage(), e1); return false; } } return false; } }