@RequestMapping(method = RequestMethod.GET, value = "/test")
@ResponseBody
@ApiOperation(httpMethod = "GET", value = "测试apo", notes = "")
public void testMail(@RequestParam(name = "host", required = false) String host,
@RequestParam(name = "port", required = false) String port,
@RequestParam(name = "userName", required = false) String userName,
@RequestParam(name = "password", required = false) String password,
@RequestParam(name = "phonenumber", required = false) String phonenumber,
@RequestParam(name = "verifyCode", required = false) String verifyCode
) {
log.info("start to send sms notification, reciever,host {},port {}, userName {} password {} destinations is {} verifyCode {}", host, port, userName, password, phonenumber, verifyCode);
try {
TCPIPConnection connection = new TCPIPConnection(host, Integer.parseInt(port));
Session session = new Session(connection);
BindRequest request = new BindTransmitter();
request.setSystemId(userName);
request.setPassword(password);
//SMPP protocol version
request.setInterfaceVersion((byte) 0x34);
session.bind(request);
String content = "[Registration]" + verifyCode + " is your verification code. Valid in 15 minutes. Please do not share this code with anyone else.";
SubmitSM submitSM = constructRequest(phonenumber, content);
SubmitSMResp response = session.submit(submitSM);
String result = new String(response.toString());
log.info("send sms notification result is {}", result);
} catch (Exception e) {
log.error("invoke sms session exception", e);
}
}
private SubmitSM constructRequest(String phoneNumber,String content) throws WrongLengthOfStringException, UnsupportedEncodingException {
String recipientPhoneNumber = phoneNumber;
SubmitSM request = new SubmitSM();
request.setDestAddr(createAddress(recipientPhoneNumber));
request.setShortMessage(content,Data.ENC_UTF8);
request.setReplaceIfPresentFlag((byte) 0);
request.setEsmClass((byte) 0);
request.setProtocolId((byte) 0);
request.setPriorityFlag((byte) 0);
request.setRegisteredDelivery((byte) 1);// we want delivery reports
request.setDataCoding((byte) 0);
request.setSmDefaultMsgId((byte) 0);
return request;
}
private Address createAddress(String address) throws WrongLengthOfStringException {
Address addressInst = new Address();
// national ton
addressInst.setTon((byte) 5);
// numeric plan indicator
addressInst.setNpi((byte) 0);
addressInst.setAddress(address, Data.SM_ADDR_LEN);
return addressInst;
}
Вот сообщение об исключении
java.io.IOException: Not connected
at org.smpp.TCPIPConnection.send(TCPIPConnection.java:340) ~[opensmpp-core-3.0.2.jar!/:na]
at org.smpp.Transmitter.send(Transmitter.java:69) ~[opensmpp-core-3.0.2.jar!/:na]
at org.smpp.Session.send(Session.java:937) ~[opensmpp-core-3.0.2.jar!/:na]
at org.smpp.Session.send(Session.java:990) ~[opensmpp-core-3.0.2.jar!/:na]
at org.smpp.Session.submit(Session.java:591) ~[opensmpp-core-3.0.2.jar!/:na]
at com.example.mq.controller.SmsController.testMail(SmsController.java:45) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]