2019独角兽企业重金招聘Python工程师标准>>>

1. 开通aliyun短信服务

获取key,密钥,短信服务接口,主题引用,创建短信模板

2. 引入依赖包

<!-- aliyun短信服务集成*****start***** --><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>2.4.2</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-sms</artifactId><version>3.0.0-rc1</version></dependency><dependency><groupId>com.aliyun.mns</groupId><artifactId>aliyun-sdk-mns</artifactId><version>1.1.8</version><classifier>jar-with-dependencies</classifier></dependency><!-- aliyun短信服务集成*****end******* -->

3. 编写集成代码

package com.lantaiyuan.ebus.common.mns;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudTopic;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.ServiceException;
import com.aliyun.mns.model.BatchSmsAttributes;
import com.aliyun.mns.model.MessageAttributes;
import com.aliyun.mns.model.RawTopicMessage;
import com.aliyun.mns.model.TopicMessage;/*** 描述:Aliyun短信服务集成* 作者:温海金* 最后更改时间:上午10:51:39*/
public class AliyunMNSHelper {private static Logger logger = LoggerFactory.getLogger(AliyunMNSHelper.class);/*** 访问短信服务的权限KEY唯一标示*/private static final String ACCESS_KEY_ID = "LTAIDlL1bylKI20O";/*** 访问短信服务的权限KEY密钥*/private static final String ACCESS_KEY_SECRET = "x7Wojdu8mEXV4hOXWr1MF8YHLxdO6P";/*** 接口访问地址*/private static final String MNS_END_POINT = "https://1054280657679380.mns.cn-hangzhou.aliyuncs.com/";/*** 华南1主题引用*/private static final String TOPIC_REF = "sms.topic-cn-hangzhou";/*** 签名名称*/private static final String SIGN_NAME = "坐公交";/*** 服务器出现异常*/public static final String SERVER_EXCEPTION = "SMS_62885094";/*** 服务器回复正常*/public static final String SERVER_BACK_TO_NORMAL = "SMS_62830031";/*** 云账号对象*/public static CloudAccount account = null;/*** 短信息客户端对象*/public static MNSClient client = null;/*** 功能描述:获取云账户信息* 作者:温海金* 最后更改时间 : 2017年4月21日 下午1:04:59*/private static CloudAccount getCloudAccount() {if(account == null) {synchronized (CloudAccount.class) {if(account == null) {account = new CloudAccount(ACCESS_KEY_ID, ACCESS_KEY_SECRET, MNS_END_POINT);}}}return account;}/*** 功能描述:获取短信服务客户端* 作者:温海金* 最后更改时间 : 2017年4月21日 下午1:19:09*/private static MNSClient getMNSClient() {if(client == null) {synchronized (MNSClient.class) {if(client == null) {client = getCloudAccount().getMNSClient();}}}return client;}/*** 功能描述:发送短信服务* 作者:温海金* 参数说明:  cityName 城市名称*          phoneNum 需要通知的手机号码* 最后更改时间 : 2017年4月21日 下午1:19:29*/public static void sendMNS(String cityName, String phoneNum, String smsTemplateCode) {/*** Step 1. 获取主题引用*/CloudTopic topic = getMNSClient().getTopicRef(TOPIC_REF);/*** Step 2. 设置SMS消息体(必须)** 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。*/RawTopicMessage msg = new RawTopicMessage();msg.setMessageBody("sms-message");/*** Step 3. 生成SMS消息属性*/MessageAttributes messageAttributes = new MessageAttributes();BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();// 3.1 设置发送短信的签名(SMSSignName)batchSmsAttributes.setFreeSignName(SIGN_NAME);// 3.2 设置发送短信使用的模板(SMSTempateCode)batchSmsAttributes.setTemplateCode(smsTemplateCode);// 3.3 设置发送短信所使用的模板中参数对应的值(在短信模板中定义的,没有可以不用设置)BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();smsReceiverParams.setParam("cityName", cityName);// 3.4 增加接收短信的号码batchSmsAttributes.addSmsReceiver(phoneNum, smsReceiverParams);messageAttributes.setBatchSmsAttributes(batchSmsAttributes);try {/*** Step 4. 发布SMS消息*/TopicMessage ret = topic.publishMessage(msg, messageAttributes);logger.info("发送短信息通知,MessageId: " + ret.getMessageId());logger.info("发送短信息通知,MessageMD5: " + ret.getMessageBodyMD5());} catch (ServiceException se) {logger.error(se.getErrorCode() + se.getRequestId());logger.error(se.getMessage());} catch (Exception e) {logger.error("发送短信服务异常!",e);}//client.close();}public static void main(String[] args) {AliyunMNSHelper.sendMNS("柳州", "18006915***", AliyunMNSHelper.SERVER_BACK_TO_NORMAL);AliyunMNSHelper.sendMNS("柳州", "18006915***", AliyunMNSHelper.SERVER_EXCEPTION);}
}

4.集成测试

转载于:https://my.oschina.net/u/2988360/blog/884639

Aliyun短信服务集成相关推荐

  1. aliyun短信服务包含随机生成四位数字验证码工具类

    .1.pom文件 <dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sd ...

  2. 短信服务之阿里云平台

    短信服务之阿里云短信平台 开通阿里云短信服务 集成到TP5框架 安装 使用 如果解决了你的疑问,麻烦点个赞吧,当然也不介意您关注收藏下的呢 开通阿里云短信服务 登录阿里云进入控制台 产品与服务–> ...

  3. springboot+springsecurity+阿里云短信服务验证实现注册登录

    使用springboot+security+Aliyun短信服务实现注册登录 为了实现个人博客部分的登录注册,我采用了阿里云短信服务发送验证码,后端比对验证码的方式完成注册,现在功能还不完全,以后这个 ...

  4. 使用阿里云短信服务测试功能发送短信到手机验证

    1.首先打开aliyun短信服务页面,点击"免签名/模板审核的API发送测试" 2.绑定你的测试手机号,点击调用API发送短信 3.进入该页面你只需要先看一看就行,等会需要把Tem ...

  5. 【微服务集成阿里SMS短信服务发送短信】

    发送短信项目中很多地方都在使用,所以集成一个单独的服务,如果某个服务需要发送短信只需要依赖短信服务即可. 1.开通阿里SMS短信,创建模板 (省略) 2.创建短信服务 common-server-sm ...

  6. Java后端集成发送短信功能(用的是阿里云的短信服务)

    首先说明,我的操作步骤主要以截图为主,基本上是每一步都有截图,所以难免显有些啰嗦,不喜勿喷.喜欢看文字的可以访问 https://help.aliyun.com/document_detail/552 ...

  7. Java常用工具类-发短信(集成阿里云短信服务)

    1.注册阿里账号并开通短信服务 注册阿里账号后,并进行个人或企业实名认证,实名认证后,才能开通短信服务. 界面如下: 2.获取AccessKey 参考获取AccessKey方法 创建成功后,如下图: ...

  8. Java 阿里云短信服务的集成

    Java 阿里云短信服务的集成 1.短信发送API(SendSms)---JAVA  [ https://help.aliyun.com/document_detail/55284.html?spm= ...

  9. SpringBoot集成阿里云短信服务

    SpringBoot集成阿里云短信服务 1.准备工作 2.项目集成 2.1 添加依赖 2.2 配置文件 2.3 业务逻辑实现 在实际项目中经常有发送短信的功能,今天进说一下对接阿里云短信服务实现短信发 ...

最新文章

  1. 不知道什么时间收集的code
  2. seci-log 1.11 发布 增加了ftpserver,远程ftp,sftp采集简化配置等功能
  3. [Nginx] Nginx 配置location总结
  4. Java获取当前路径和读取文件
  5. python保存表格_python怎么把数据保存为excel
  6. how to undo git commit
  7. 如何“神还原”数据中心? 阿里联合NTU打造了工业级精度的仿真沙盘!
  8. 【Level 08】U06 Good Feeling L4 The surprising event
  9. CS224N刷题——Assignment2.1_TensorflowSoftmax
  10. Balrum 汉化指引
  11. PCB的paste与solder层
  12. HTML入门学习教程:简单网页制作
  13. 世界水日|“彩绘春天 守护长江”亲子环保公益顺利举行
  14. python 内建排序 HOW TO
  15. Reading Ingestion —— Bigtable: A Distributed Storage System for Structured Data
  16. mysql最优库容_一种增加已达到设计库容的尾矿库存储量的方法
  17. 全栈创新加速“算力网络”时代来临:英特尔携手中国移动共谱数智华章
  18. 这里有一个“天梯排行榜”,来看看你在哪一级?
  19. 如何系统性学习IT技术
  20. [转载]Win7中的页目录

热门文章

  1. MUR860D-ASEMI快恢复二极管MUR2060AC
  2. Python中Tkinter解决button的command无返回值问题
  3. 【初入前端】第一课 课前预习
  4. PCIe扫盲——PCIe错误源详解(二)
  5. fish-ui 一套基于vue2的ui组件库 1
  6. 你是否了解AR技术?AR技术就在我们身边
  7. Windows CE下中文输入法编辑器
  8. 1497: 变态杀人狂
  9. MySQL单表查询练习题
  10. [NOI2005]月下柠檬树 (自适应辛普森)