近来做了一个电信项目,电信要求使用它们的短信接口SMGP进行短信通知.找了很多方法, 刚开始乱码,各种处理,后来通过smgpapi20100113.jar,以及其源码已经完了功能的实现。特此记录;
smgpapi20100113.jar及其源码下载地址:
或我本站分享:http://download.csdn.net/detail/pd10340927/9402069
电信分为长短信和短短信,具体实现如下:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
import Sample.Config;
import cn.com.zjtelecom.smgp.Client;
import cn.com.zjtelecom.smgp.bean.Result;
import cn.com.zjtelecom.smgp.bean.Submit;
public classSmgpUtil {
private static final String CONFURL = "config/smgp.properties";
private static final String smgpHost = getConfigValue("smgpHost");//短信接口服务器ip
private static final String smgpPort = getConfigValue("smgpPort");//服务器端口
private static final String smgpUser = getConfigValue("smgpUser");//用户名
private static final String smgpPwd = getConfigValue("smgpPwd");//密码
private static final String smgpNo = getConfigValue("smgpNo");//电信提供的发送号码
/**
* 发送长短信
* @param phoneNo 接收端号码
* @param msg 消息内容
* @throws IOException
*/
public static void doSendLongMsg(String phoneNo,String msg) throws IOException{
//soid一般指你的消息来源,如测试,或者来自XX项目的什么功能,参数传2和传0 是制定电信本次socket连接的具体用法,如发短信,收短信,还是其他
Client client = new Client(smgpHost, Integer.valueOf(smgpPort.trim()), 2, smgpUser, smgpPwd, "spid", 0);
Submit submit = new Submit();
submit.setSrcTermid(smgpNo);
submit.setDestTermid(phoneNo);
//短消息内容体的编码格式。
//0=ASCII 编码;
//3=短消息写卡操作;
//4=二进制短消息;
//8=UCS2 编码;
//15=GB18030 编码;
//246(F6)=UIM 相关消息 UIM 相关消息,用于与 UIM 卡相关的 OTA 等业务,终端
//收到该类型消息直接转发给 UIM 卡,由 UIM 卡来处理该类型消息;
//其它保留。
//对于文字短消息,要求 MsgFormat=15。对于回执消息,要求 MsgFormat=0。
submit.setMsgFormat(15);
submit.setMsgContent(msg.getBytes("GB18030")); //将字符集编码和你设置的msgFormat对应
submit.setProductID("产品ID");//可以为空
submit.setMsgType(0);
submit.setNeedReport(0);
submit.setPriority(0);
//serviceId表示业务代码,是该条短消息所属的业务类别,由数字、字母
//和符号组合而成。对于从 WEB 上发送的点对点短消息,要求业务代码为 “PC2P” ,其它业务代码由 SP 自定义。
submit.setServiceID("PC2P");
// 发送短信
Result[] result = client.SendLong(submit);
for (int i = 0; i < result.length; i++) {
System.out.println("--------------------------------");
System.out.println("Message "+i+"");
System.out.println("Status:"+ result[i].ErrorCode);
System.out.println("MsgID:"+ result[i].ErrorDescription);
System.out.println("--------------------------------");
}
// 退出
client.Close();
}
/**
* 发送短短信
* @param phoneNo 接收端号码
* @param msg 消息内容
* @throws IOException
*/
public static void doSendShortMsg(String phoneNo,String msg) throws IOException{
//初始化client
Clientclient= newClient(smgpHost,Integer.valueOf(smgpPort.trim()), 2, smgpUser, smgpPwd,"spid",0);
//设置submit
Submitsubmit=newSubmit();
submit.setSrcTermid(smgpNo);
submit.setDestTermid(phoneNo);
submit.setProductID("产品ID");//可以为空
submit.setMsgType(0);
submit.setNeedReport(0);
submit.setPriority(0);
submit.setServiceID("PC2P");
submit.setMsgFormat(15);
submit.setMsgContent(msg.getBytes("GB18030"));
Result result =client.Send(submit);
System.out.println("Status:"+result.ErrorCode);
System.out.println("MsgID:"+result.ErrorDescription);
//退出
client.Close();
}
privatestaticString getConfigValue(String key) {
Propertiesconfig= newProperties();
InputStreamin = null;
try {
config.load(newClassPathResource(CONFURL).getInputStream());
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
if (in != null){
try {
in.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
return config.getProperty(key);
}
}