首先要导入阿里云的依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
编写工具类
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SmsUtils
{
//产品名称:阿里云短信API产品,开发者无需替换
private static final String product = "Dysmsapi";
//产品域名,开发者无需替换
private static final String domain = "dysmsapi.aliyuncs.com";
/**
* 阿里短信服务接口
*
*/
public static SendSmsResponse sendSms(String accessKeyId, String accessKeySecret,String phone ,String signName ,String templateCode,String data) {
SendSmsResponse sendSmsResponse = null;
try {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
System.setProperty("sun.net.client.defaultReadTimeout", "5000");
//初始化acsClient,暂不支持region化
/ //accessKeyId 控制台找
//accessKeySecret 在创建用户的时候与上面的KeyId一起出现,只出现一次,第一次要记得保存,否则后面找不到需要重新创建
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台
SendSmsRequest request = new SendSmsRequest();
//必填:要发送的手机号
request.setPhoneNumbers(phone);
//必填:短信签名-在短信控制台中找
request.setSignName(signName);
//必填:短信模板-在短信控制台中找
request.setTemplateCode(templateCode);
//可选:要发送的内容,这里data要注意格式,是map格式转为String的
request.setTemplateParam(data);
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId("0");
//此处可能会抛出异常
sendSmsResponse = acsClient.getAcsResponse(request);
} catch (ClientException e) {
log.error("-----------调用失败--------------");
}
return sendSmsResponse;
}
可写个测试方法调试
public static void main(String[] args) {
String accessKeyId = "LTxxxxxxxxXXXXXXXUAQ";
String accessKeySecret = "rZNxxxxxxxxXXXXXXXXKC";
String phone = "13xxxxxxx41";
String signName = "xxxx";
String templateCode = "SMS_xxxxxxx";
Map<String, String> map = new HashMap<String, String>();
map.put("code", "888888");
JSONObject obj = JSONObject.fromObject(map);
String data = obj.toString();
try {
SendSmsResponse sendSmsResponse = SmsUtils.sendSms(accessKeyId,accessKeySecret,phone,signName,templateCode,data);
System.out.println("-----------------success------------");
} catch (Exception e) {
System.out.println("--------------fail--------"+e);
}
}
注意:templateCode短信模块里面的内容,例如:您有新的订单待处理,当前状态:${status},订单摘要:${remark},请及时处理。
这里有两个参数要传,map.put("status","xxxx"),map.put("remark","xxx"),这里map的key键一定要对应模板的${status}
