`

国外短信发送接口

    博客分类:
  • java
阅读更多

最近自己在弄一个英国的优惠券的网站!需要用到国外的短信发送接口!

 

搜索和查找发现有几个比较不错的短信发送接口网站!

 

代码粘出来给大家分享一下!

 

 

SMSService  抽象接口类
/**
 * squarelife
 * 
 *  @author eric
 *
 *  2011-11-28  下午08:41:43
 */
package com.life.service.sms;

/**
 * @author eric
 *
 * 2011-11-28  下午08:41:43
 */
public interface SMSService {
	boolean sendMessage(String msg,String countryCode,String phoneNumber);
}
 
需要配置的短信配置信息! sms.properties

#短信接口名称Gateway160SMSServiceImpl,BulksmsSmsServiceImpl
#Gateway160SMSServiceImpl,test123456,test123456, 065af763-bdb8-4f27-bc81-d6c14f2f545c
#http://www.bulksms.co.uk/
#http://www.gateway160.com/
wy.sms.service.name=Gateway160SMSServiceImpl
#短信接口的用户名
wy.sms.service.accountName=****
#短信接口国家代码
#Gateway160SMSServiceImpl  用国家代码如中国:CN 美国:US  英国:GB   
#BulksmsSmsServiceImpl     用电话代码如中国:86 美国:1   英国:44
wy.sms.service.countryCode=CN
#短信接口的密码
wy.sms.service.password=****
#短信接口APIkey 注册时候的APIkey 
wy.sms.service.apiKey=****
 
package com.life.service.sms;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.life.util.PropertyUtil;
/**
 * 
 * @author eric
 *
 * 2011-11-28  下午09:15:23
 */
public class AbstractSMSService {
	/**
	 * 获取属性值
	 * 2011-11-28 下午09:15:27
	 * @param key
	 * @return
	 */
	protected String getProperty(String key) {
		return new PropertyUtil().getProperty(key);		
	}
	
	/**
	 * 2011-11-28 下午10:28:57
	 * @param params
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	protected String encode(final HashMap<String,String> params) throws UnsupportedEncodingException {
	        StringBuilder sb = new StringBuilder();         
	        for (Map.Entry<String, String> entry : params.entrySet()) {
	            String key = entry.getKey();
	            String val = entry.getValue();
	            sb.append(URLEncoder.encode(key, "UTF-8"));
	            sb.append("=");
	            sb.append(URLEncoder.encode(val, "UTF-8"));
	            sb.append("&");
	        } 
	        return sb.toString();
	    }
}
 

 

 

 

 

BulksmsSmsServiceImpl   短信接口实现类 
接口相关信息网址
   
http://www.bulksms.co.uk/
 

 

/**
 * squarelife
 * 
 *  @author eric
 *
 *  2011-11-28  下午09:33:18
 */
package com.life.service.sms.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.life.service.sms.AbstractSMSService;
import com.life.service.sms.SMSService;

/**
 * @author eric
 * 
 *         2011-11-28 下午09:33:18
 */
public class BulksmsSmsServiceImpl extends AbstractSMSService implements
		SMSService {
	private static final Logger logger = Logger
			.getLogger(BulksmsSmsServiceImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.life.service.sms.SMSService#sendMessage(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public boolean sendMessage(String msg, String countryCode,
			String phoneNumber) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("username", getProperty("wy.sms.service.accountName"));
		params.put("password", getProperty("wy.sms.service.password"));
		params.put("phoneNumber", phoneNumber);
		params.put("message", msg);
		params.put("want_report", "1");
		params.put("msisdn", countryCode + phoneNumber);
		URL url = null;
		HttpURLConnection httpCon = null;
		try {
			// Construct data
			// String data = "";
			/*
			 * Note the suggested encoding for certain parameters, notably the
			 * username, password and especially the message. ISO-8859-1 is
			 * essentially the character set that we use for message bodies,
			 * with a few exceptions for e.g. Greek characters. For a full list,
			 * see:
			 * http://www.bulksms.co.uk/docs/eapi/submission/character_encoding/
			 */
			// Send data
			url = new URL(
					"http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0");
			/*
			 * If your firewall blocks access to port 5567, you can fall back to
			 * port 80: URL url = new
			 * URL("http://www.bulksms.co.uk/eapi/submission/send_sms/2/2.0");
			 * (See FAQ for more details.)
			 */
			httpCon = (HttpURLConnection) url.openConnection();
			httpCon.setDoOutput(true);
			httpCon.setRequestMethod("POST");
			httpCon.setRequestProperty("Content-type", "text/html");
			httpCon.setRequestProperty("Accept-Charset", "UTF-8");
			httpCon.setRequestProperty("contentType", "UTF-8");
			// write post body
			OutputStreamWriter out = new OutputStreamWriter(httpCon
					.getOutputStream(), "UTF-8");
			String postBody = encode(params);
			out.write(postBody);
			out.flush();
			out.close();

			// Get the response
			StringBuilder sb = new StringBuilder();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					httpCon.getInputStream()));
			String line;
			while ((line = rd.readLine()) != null) {
				sb.append(line);
			}
			rd.close();
			logger.info("responseCode=" + httpCon.getResponseCode());
			logger.info("responseBody=" + sb.toString());
			if (httpCon != null
					&& httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			logger.error("Exception throw!", e);
			return false;
		} finally {
			if (httpCon != null) {
				httpCon.disconnect();
			}
		}
	}

}
 

 

Gateway160SMSServiceImpl 
接口相关信息网址

http://www.gateway160.com/

 

/**
 * squarelife
 * 
 *  @author eric
 *
 *  2011-11-28  下午08:43:23
 */
package com.life.service.sms.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.life.service.sms.AbstractSMSService;
import com.life.service.sms.SMSService;

/**
 * @author eric
 * 
 *         2011-11-28 下午08:43:23
 */
public class Gateway160SMSServiceImpl extends AbstractSMSService implements
		SMSService {
	private static final Logger logger = Logger
			.getLogger(Gateway160SMSServiceImpl.class);

	@Override
	public boolean sendMessage(String msgContent, String countryCode,
			String phoneNumber) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("accountName", getProperty("wy.sms.service.accountName"));
		params.put("key", getProperty("wy.sms.service.apiKey"));
		params.put("phoneNumber", phoneNumber);
		params.put("message", msgContent);
		params.put("countryCode", countryCode);
		try {
			URL url = new URL("http://api.gateway160.com/client/sendmessage/");
			HttpURLConnection httpCon = (HttpURLConnection) url
					.openConnection();
			httpCon.setDoOutput(true);
			httpCon.setRequestMethod("POST");
			// write post body
			OutputStreamWriter out = new OutputStreamWriter(httpCon
					.getOutputStream());
			String postBody = encode(params);
			out.write(postBody);
			out.flush();
			out.close();

			// Get the response
			StringBuilder sb = new StringBuilder();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					httpCon.getInputStream()));
			String line;
			while ((line = rd.readLine()) != null) {
				sb.append(line);
			}
			rd.close();

			logger.info("responseCode=" + httpCon.getResponseCode());
			logger.info("responseBody=" + sb.toString());
			if (httpCon != null
					&& httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
				logger.info("连接成功!");
				if (sb != null && sb.equals("1")) {
					logger.info("短信发送成功!");
					return true;
				} else if (sb != null && sb.equals("0")) {
					logger.info("invalid account name or key");
					return false;
				} else if (sb != null && sb.equals("-1")) {
					logger.info("短信发送失败!");
					return false;
				} else {
					return false;
				}
			} else {
				logger.info("连接失败!");
				return false;
			}
		} catch (IOException e) {
			logger.error("IOException thrown! ", e);
			return false;
		}
	}

}
 

 

给出测试类!相关的PropertiesUtil.java文件就不提供了。一般大家都会写吧!如果有实在有问题的可以联系我!

 

package com.life.test;

import org.junit.Test;

import com.life.service.sms.SMSService;
import com.life.service.sms.impl.BulksmsSmsServiceImpl;
import com.life.service.sms.impl.Gateway160SMSServiceImpl;

/**
 * 
 * @author eric
 *
 * 2011-11-28  下午10:01:35
 */
public class SMSTest {
	@Test
	public void testSendMsg(){
//		SMSService smsService=new BulksmsSmsServiceImpl();
//		smsService.sendMessage("亲爱的,我爱你!", "86", "**********");
//	    
//	    SMSService smsService=new Gateway160SMSServiceImpl();
//	    smsService.sendMessage("亲爱的,我爱你! ", "CN", "**********");
//	    
//	    SMSService smsService=new Gateway160SMSServiceImpl();
//	    smsService.sendMessage("哈哈,你这么肯定是我发的呀,你好好上课吧,不打扰你上课了,上课加油!!三明治加油!!", "CN", "*******");
		
		SMSService smsService=new Gateway160SMSServiceImpl();
		smsService.sendMessage("你莫冤枉我撒,又冤枉我,老是冤枉我,我承认个啥呀,嘎", "CN", "*********");
	}
}

 

大家注意不同的短信接口需要的国家代码格式会有所不同,测试类可以看到。。相关国家代码可以去查看相关网页,百度,谷歌都可以查到。你懂的!另外附上国家代码查看文档!见附件

 

 

 

0
3
分享到:
评论
4 楼 wangyong31893189 2012-04-09  
y_lj2003 写道
非洲那边项目,需要能发送短信,这个接口能用么?还是去找他们那边的

你看下国家代码,里是否有非洲国家的,如果有也是可以用的!前提请注册一个帐号密码
3 楼 y_lj2003 2012-03-14  
非洲那边项目,需要能发送短信,这个接口能用么?还是去找他们那边的
2 楼 wangyong31893189 2011-12-16  
rensanning 写道
使用Clickatell(API) 不是更简单嘛!
http://www.clickatell.com/

当然我也没有说我的这个接口就写得很简单了,只是供大家一个参考而已,如果有要用到的可以参考一下,有更简单的当然很好了啊!呵呵。。可以交流交流撒!
1 楼 rensanning 2011-12-11  
使用Clickatell(API) 不是更简单嘛!
http://www.clickatell.com/

相关推荐

    Java 调用短信API接口

    摩杜云短信业务接入,该平台支持国内和国际快速发送验证码、短信通知和推广短信,服务范围覆盖全球200多个国家和地区。国内短信支持三网合一专属通道,与工信部携号转网平台实时互联。...完美支撑8亿短信发送。

    短信平台API接口

    亿美短信应用API接口-八项创新  1、全网覆盖:全国全网、电信、联通、移动显示同一号码  2、心跳机制:保证客户端与服务器时时连接  3、异步通讯:支持异步通讯,每个连接峰值可达20条/秒  4、智能化短信...

    PHP 调用短信API接口

    摩杜云短信业务接入,该平台支持国内和国际快速发送验证码、短信通知和推广短信,服务范围覆盖全球200多个国家和地区。国内短信支持三网合一专属通道,与工信部携号转网平台实时互联。...完美支撑8亿短信发送。

    GOOGLE VOICE无限短信接口 程序 v2.0.rar

    免费发送无限短信的接口 通过Google voice的服务你可以把短信(SMS)发送到世界上任何手机.而且这项服务是免费的,可靠的 你需要一个google voice的账号即可。

    《月影短信》免费编程接口(DLL)

    §支持各大短信联盟、自定义短信联盟代号、手机号码批量发送、国际区号、自定义落款等等,与任何硬件设备完全无关,也不依赖任何手机或单一平台。 §支持电子邮件特快专递,自动监测当前DNS、MX服务器,支持发送...

    无心版移动短信发送 v1.2

    无心版移动短信发送v1.2升级说明: 1、新版程序采用UTF-8编码,完全与国际接轨。 2、去除每条短信后面的签名,您可以自由设置自己的签名。 3、新版程序采用新的服务器,更加稳定。 无心版移动短信发送v1.2使用...

    2013免费发送短信

    2013免费发送短信工具 利用国外的接口 验证码识别率不是很高哦

    .net core 阿里云短信服务SDK

    完美支撑双11期间20亿短信发送,6亿用户触达。 使用其短信服务要先申请access key,短信签名和短信模板,审核时间一般一到两个工作日就可以了。 然后确保账户有余额,一条通知短信0.0045元。 随后就可以调用API...

    Python基于Twilio及腾讯云实现国际国内短信接口

    短信服务验证服务已经不是什么新鲜事了,但是免费的手机短信服务却不多见,本次利用Python3.0基于Twilio和腾讯云服务分别来体验一下国际短信和国内短信接口。 首先,注册Twilio: www.twilio.com/ 注册成功后,获取...

    手机短信平台软件开发

    提供短信平台,sp平台软件(支持三网),企信通平台. 运营商,电视台,报业集团,气象局,各行业企业,单位,sp等等电信增值业务运营公司 专用的著名稳定,短信平台,彩信平台软件 帮助测试接入移动,联通,电信运营...

    无心短信发送程序 1.2

    无心短信发送程序能使用此程序发送短信到移动手机用户,支持最多发送350字,按标准短信资费0.1元/条计费注:由于移动限制,每一个邮箱每天仅能发送50条短信。 无心短信发送程序 1.2 升级说明: 1、新版程序采用...

    短信收发控件为标准的AcitveX控件

    支持GSM短信发送,中英文短信息发送,支持发送闪烁短信、免提短信。 支持BIT7格式发送(即对于英文,长度可达160个字符) 支持WAP PUSH发送 支持状态回复 支持小灵通号码发送 支持国际手机号码发送 接收短信时...

    快客通短信邮件营销平台 v1.0.0

    2、邮件通道是由国际顶尖的几家EDM数据公司提供的通道,发送成功率90%左右;3、短信和邮件提供API(SDK)程序接口,方便网站和软件开发商进行系统集成开发;4、软件方便用户对自己的客户进行分组、信息管理。5、软件...

    全球短信免费发1.0(只能发英文)

    基于云计算技术,DegreeSMS是美国最大的免费短信服务,允许短信发送通过免费网站上的任何手机运营商。 (以上为词霸自动翻译结果) 免责:由于程序是利用其他网站的接口制作,故随时有可能因为该网站变动而失效,...

    OCX.rar_alasunsmscon.ocx_push_sms_sms and receive_sms ocx

    支持BIT7格式发送(即对于英文,长度可达160个字符) 支持WAP PUSH发送 支持状态回复 支持小灵通号码发送 支持国际手机号码发送 接收短信时,可自行选择是否将短信保存于SIM卡中 提供At Command接口,通过这一接口...

    易语言-易语言调用腾讯云发送短信例子

    接口说明 给用户发短信验证码、短信通知,营销短信(内容长度不超过450字)。 注:sdkappid请填写您在腾讯云上申请到的,random请填成随机数。 2 请求包体 包体为json字符串,参数如下: { "tel": { //如需使用国际...

    sp短信平台软件资料

    提供短信平台,彩信平台软件,性能稳定.全国品牌SP平台软件. 运营商,电视台,报业集团,气象局, sp等等电信增值业务运营公司的专用 电信增值管理平台软件。 联系:QQ:84401116 电话:13950404552 许风 短信平台: 1、...

    快客通短信邮件营销平台 v1.0.0.zip

    快客通是一款集短信、邮件营销接口的小型免费CRM系统,适用于电商(淘宝、天猫、拍拍、京东及传统B2C)、学校、医院、汽车4S店、餐饮娱乐等各行各业。 软件特点: 1、高质量106短信通道,在手机号码无误的情况下...

Global site tag (gtag.js) - Google Analytics