实现阿里云云通讯发送短信验证码,首先须要在阿里云平台申请短信签名、短信模板、accessKeyId和accessKeySecret。html
具体信息以下:
短信模板:“您的金库验证码为${code},3分钟有效,打死不要告诉劫匪哦!”
短信模板code:“SMS_142953888”
短信签名:“来钱快”
AccessKeyID : “LTAI85Zw4Byqfuck”
AccessKeySecret: L7j2alfhjF6lXG8YReV2ho9106fuckjava
依赖项:spring
可下载官方sdk包(https://help.aliyun.com/document_detail/55359.html?spm=a2c4g.11186623.2.8.3b1e5777IIZpea),
或者加入maven依赖以下:api
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.7.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency>
具体代码参照官方demo,以下:dom
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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; /** * Created on 17/6/7. * 短信API产品的DEMO程序,工程中包含了一个SmsDemo类,直接经过 * 执行main函数便可体验短信产品API功能(只须要将AK替换成开通了云通讯-短信产品功能的AK便可) * 工程依赖了2个jar包(存放在工程的libs目录下) * 1:aliyun-java-sdk-core.jar * 2:aliyun-java-sdk-dysmsapi.jar * <p> * 备注:Demo工程编码采用UTF-8 * 国际短信发送请勿参照此DEMO */ @Component public class SmsSender { private final Logger logger = LoggerFactory.getLogger(this.getClass()); //产品名称:云通讯短信API产品,开发者无需替换 private final String product = "Dysmsapi"; //产品域名,开发者无需替换 private final String domain = "dysmsapi.aliyuncs.com"; //短信签名 @Value("${sms.sign.name}") private String signName; //短信模板 @Value("${sms.template.code}") private String templateCode; @Value("${sms.access.key.id}") private String accessKeyId; @Value("${sms.access.key.secret}") private String accessKeySecret; /* * sendSms * @description:短信 * @author 李阳 * @date 2018/8/31 * @params [mobile, code] * @return com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse */ public SendSmsResponse sendSms(String mobile, String code) { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); SendSmsResponse sendSmsResponse = null; try { //初始化acsClient,暂不支持region化 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(mobile.trim()); //必填:短信签名-可在短信控制台中找到 request.setSignName(signName); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(templateCode); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的god,您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"code\":\"" + code + "\"}"); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) // request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 // request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch sendSmsResponse = acsClient.getAcsResponse(request); } catch (ClientException e) { logger.error(e.getMessage(), e); } return sendSmsResponse; } public static void main(String[] args) throws ClientException, InterruptedException, UnsupportedEncodingException { //发短信 SendSmsResponse response = new SmsSender().sendSms("19916417737", "88888"); System.out.println("短信接口返回的数据----------------"); System.out.println("Code=" + response.getCode()); System.out.println("Message=" + response.getMessage()); System.out.println("RequestId=" + response.getRequestId()); System.out.println("BizId=" + response.getBizId()); } }
短信内容以下:
【来钱快】您的金库验证码为8888,3分钟有效,打死不要告诉劫匪哦!maven
可参考官网:https://help.aliyun.com/document_detail/55284.html?spm=5176.10629532.106.1.2ba31cbef9Zu6c函数