JAVA-阿里云短信服务

转载请注明出处

1、引入相关依赖

<!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.2</version>
        </dependency>
        
        <!--短信-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.0.6</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 -->
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>

2、JAVA代码

import cn.hutool.json.JSONUtil;
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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.ruoyi.kkxapi.common.service.H5CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class H5CommonServiceImpl implements H5CommonService {

    /**
     * 发送短信
     *
     * @return
     */
    @Override
    public SendSmsResponse sendSms(String phoneNumbers, String templateCode, Map<String, Object> map) throws Exception {
        //设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        final String product = "Dysmsapi";
        final String domain = "dysmsapi.aliyuncs.com";
        final String accessKeyId = "----";
        final String accessKeySecret = "----";
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setMethod(MethodType.POST);
        //必填:待发送手机号。支持以逗号分隔的形式进行批量调用
        sendSmsRequest.setPhoneNumbers(phoneNumbers);
        sendSmsRequest.setSignName("----");
        sendSmsRequest.setTemplateCode(templateCode);
        sendSmsRequest.setTemplateParam(JSONUtil.toJsonStr(map));
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sendSmsRequest);
        log.info("------" + phoneNumbers + "--------" + templateCode + "---短信返回结果---" + sendSmsResponse.getCode() + sendSmsResponse.getMessage());
        return sendSmsResponse;
    }
}

3、测试

map中的key对应短信模板中的变量属性

@Test
public void smsTest() throws Exception {
    //获取短信模板CODE
    String appConfigCode = "SMS_PSM";
    AppConfig appConfig = appConfigService.getConfigByCode(appConfigCode);
    //短信模板参数
    Map<String, Object> map = new HashMap<>();
    String code = String.valueOf((int) (Math.random() * 900000 + 100000));
    map.put("code", code);
    map.put("name", "李四");
    commonService.sendSms("----", appConfig.getAValue(), map);
}

原文 

http://www.cnblogs.com/binaryStar/p/12545817.html

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » JAVA-阿里云短信服务

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址