1、与阿里巴巴谈个生意,申请阿里巴巴的短信服务。这个我就不写了,按照阿里巴巴官网的申请步骤,一步一步,不会了打阿里巴巴的客服电话。
2、复制一下代码:
/**
* Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
* 功能描述:
* @Package: com.newtouch.modules.sys.utils
* @author: admin
* @date: 2019年5月17日 下午3:13:23
*/
package com.newtouch.modules.sys.utils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/**
* Copyright: Copyright (c) 2019 LanRu-Caifu
*
* @ClassName: SendSMSUtils.java
* @Description: alibaba发送短信
* @version: v1.0.0
* @author: admin
* @date: 2019年5月17日 下午3:13:23
*/
public class SendSMSUtils {
private final static String accessKeyId = "";// 你的accessKeyId
private final static String accessKeySecret = "";// 你的accessKeySecret
private final static String signName = "";你的signName
public static String sendSMS(String phoneNumber, String templateParam, String templateCode) {
DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("SignName", signName);
request.putQueryParameter("PhoneNumbers", phoneNumber);
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", templateParam);
CommonResponse response = null;
try {
response = client.getCommonResponse(request);
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println(response.getData());
return response.getData();
}
}
3、调用接口:
String templateParam = “{name:”"+applyOnline.getName()+""}";
SendSMSUtils.sendSMS(applyOnline.getMobilephone(), templateParam, “你的templateCode”);
**注:**上面穿的参数,除了电话和参数是你自己的,其他的都是在阿里巴巴的短信平台生成的。要使用这个服务,你的服务必须能访问外网,不能访问外网,就要考虑其他方式实现。