java ssm项目用sms实现短信通知发送
sms短信通知
1.Maven jar包
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->`
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency> `
2.java代码
package com.util;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class SendMessage {
public static void main(String[] args) throws Exception {
String content="通知您:订单已发货请登录《**订单进度查询系统》查询发货订单,并及时查收订单货物。";
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn");
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");// 在头文件中设置转码
NameValuePair[] data = { new NameValuePair("Uid", "****"),// 注册的用户名
new NameValuePair("Key", "d41d8cd****0b204e980"),// 注册成功后,登录网站后得到的密钥
new NameValuePair("smsMob", "1313588****"),// 手机号码
new NameValuePair("smsText", content) };// 短信内容
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
for (Header h : headers) {
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println(result); // 打印返回消息状态
post.releaseConnection();
}
}
因公司需要发送短信通知所以我在网上找了些,自己改了一点,本人亲测有效,现在正在纠结用SMS短信通还是用榛子云短信,榛子便宜一点,在纠结中。