发送短信参看文档
阿里云短信参考文档:阿里云参考文档
中国移动mas短信参看文档 :主页下载 主页
发送模板短信代码
阿里云发送模板短信代码
需要申请模板,通过审核才可以用
1.添加依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>dysmsapi20170525</artifactId> <version>2.0.9</version> </dependency>
2.编写发送短信的工具类
import com.aliyun.dysmsapi20170525.Client
import com.aliyun.dysmsapi20170525.models.SendSmsRequest
import com.aliyun.teautil.models.RuntimeOptions
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
@Component
class AliSmsHelper {
@Value("\${sms-sending.access-key-id}")
private var accessKeyId: String? = null
@Value("\${sms-sending.access-key-secret}")
private var accessKeySecret: String? = null
/**
* 发送单条短信
*
* @param phone 目标手机号
* @param paramsStr 参数字符串
* @param templateCode 模板代码
* @param signName 签名
*/
@Throws(Exception::class)
fun sendSms(phone: String?, paramsStr: String?, templateCode: String?, signName: String?) {
val client = createClient(accessKeyId, accessKeySecret)
val sendSmsRequest = SendSmsRequest()
.setPhoneNumbers(phone)
.setSignName(signName)
.setTemplateCode(templateCode)
.setTemplateParam(paramsStr)
client.sendSmsWithOptions(sendSmsRequest, RuntimeOptions())
}
@Throws(Exception::class)
private fun createClient(accessKeyId: String?, accessKeySecret: String?): Client {
val config = com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret)
// 访问的域名,固定即可
config.endpoint = "dysmsapi.aliyuncs.com"
return Client(config)
}
}
中国移动发送模板短信代码
可以自定义发送短信内容,无需审核
发送http类型的接口文档(http://mas.10086.cn主页下载)
创建接口操作(短信接口创建 --登录–>管理–>接口管理)
2.编写发送短信的工具类
import cn.hutool.http.HttpRequest
import com.alibaba.fastjson.JSON
import com.cgs.hb.xtns.dto.SendReq
import com.cgs.hb.xtns.dto.SendRes
import com.cgs.hb.xtns.utils.JSONUtils.json2pojo
import com.cgs.hb.xtns.utils.Md5Util.MD5
import org.apache.commons.codec.binary.Base64
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import java.util.*
/**
* @desc 移动云mas发送模板短信类
*/
@Component
class SMSClient {
@Value("\${mas.apId}")
private val apId: String? = null
@Value("\${mas.secretKey}")
private val secretKey: String? = null
@Value("\${mas.ecName}")
private val ecName: String? = null
@Value("\${mas.sign}")
private val sign: String? = null
@Value("\${mas.addSerial}")
private val addSerial: String? = null
@Value("\${mas.url}")
private val url: String? = null
/**
* @desc 发送短信(模板短信)
* @param templateId 模板id
* @param params 参数
* @param mobiles 电话
*/
fun sendMsg(templateId: String, params: String, mobiles: String): Int {
//设置请求体信息
val sendReq = SendReq()
sendReq.apId = apId
sendReq.ecName = ecName
sendReq.secretKey = secretKey
sendReq.mobiles = mobiles
sendReq.addSerial = addSerial
sendReq.sign = sign
sendReq.templateId = templateId
sendReq.params = params
//按顺序拼接,加密获取mac
val stringBuffer = StringBuffer()
stringBuffer.append(sendReq.ecName)
stringBuffer.append(sendReq.apId)
stringBuffer.append(sendReq.secretKey)
stringBuffer.append(sendReq.templateId)
stringBuffer.append(sendReq.mobiles)
stringBuffer.append(sendReq.params)
stringBuffer.append(sendReq.sign)
stringBuffer.append(sendReq.addSerial)
//mac:通过MD5(32位小写)计算出的值
sendReq.mac = MD5(stringBuffer.toString())!!.lowercase(Locale.getDefault())
//将对象转成json字符串
val reqText = JSON.toJSONString(sendReq)
//base64加密传输
val encode = Base64.encodeBase64String(reqText!!.toByteArray(Charsets.UTF_8))
//此处使用hutool工具包httpRequest工具类发送https请求
val resStr: String = HttpRequest.post(url)
.header("contentType", "utf-8")
.body(encode)
.execute()
.body()
//发送短信结果
val sendRes = json2pojo(resStr, SendRes::class.java)
//判断是否发送成功(成功-1 失败-0)
return if (sendRes!!.success && "" != sendRes.msgGroup && "success" == sendRes.rspcod) {
1
} else {
0
}
}
}
/**
* 发送短信请求实体-短信模板
*/
class SendReq(
//企业名称
var ecName: String? = null,
//接口账号用户名
var apId: String? = null,
//密码
var secretKey: String? = null,
//收信手机号码:英文逗号分隔,每批次限5000个号码
var mobiles: String? = null,
//模板变量
var params: String? = null,
//签名编码:在云MAS平台『管理』→『接口管理』→『短信接入用户管理』获取。
var sign: String? = null,
//扩展码,根据向移动公司申请的通道填写,如果申请的精确匹配通道,则填写空字符串(""),否则添加移动公司允许的扩展码。
var addSerial: String? = null,
//参数校验序列,生成方法:将ecName、apId、secretKey、templateId、mobiles、params、sign、addSerial按序拼接(无间隔符),通过MD5(32位小写)计算出的值。
var mac: String? = null,
//模板ID:在云MAS平台创建模板,路径:『短信』→『模板短信』→『模板管理』,创建后提交审核,审核通过将获得模板ID。
var templateId: String? = null,
)
/**
* 发送短信响应实体
*/
class SendRes(
//响应码
var rspcod: String? = null,
//消息批次号,由云MAS平台生成,用于验证短信提交报告和状态报告的一致性(取值msgGroup)注:如果数据验证不通过msgGroup为空
var msgGroup: String? = null,
//数据校验结果
var success: Boolean = false
)
apId是接口账号用户名
secretKey是接口创建的密码
url是需要问客服要的,每个企业的数据传输连接地址不一样的
#配置yml
#移动云mas发送短信
mas:
#接口账号用户名
apId:
#密码
secretKey:
#企业名称
ecName:
#签名编码
sign:
#HTTP模板短信地址
url:
#扩展码
addSerial: ""
参考文档:Java实现阿里云短信发送功能