1. Introduce Maven into the project
There are corresponding SDKs for different programming languages, just download what you need.
Add maven coordinates to pom.xml
<!--阿里云短信服务-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
<version>2.0.22</version>
</dependency>
2. Business code
The following code is not simply using the sending code provided by the SMS service provider to complete the sending of the SMS. It is too simple. I don’t think you want to read such articles. CSDN grabs a lot, or just read the official documents. . For general SMS service providers, it can be SMS services provided by Tencent Cloud, Alibaba Cloud, etc.
The update of this article has been postponed for more than ten days, and there is no time to update it. The business code and project have been completed and I put it on gitee. At that time, I will make the warehouse public, so that you can also pull it down and change it to the corresponding one. SMS sending system.
project use
The following is to start talking about the data that needs to be configured for the project to be used out of the box.
system requirement
- JDK >= 1.8
- MySQL >= 5.7
- Maven >= 3.0
necessary configuration
- Modify the database connection, edit
resources
the directoryapplication-dev.yml
# 应用服务 WEB 访问端口
server:
#应用的端口号
port: 8080
servlet:
#应用的访问路径
context-path: /
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/itmei-sms?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
username: root
password: root
doc
There are ready-to-useitmei-sms.sql
files in the directory, and then create a library to import sql into
- To modify the SMS service provider configuration information, edit
resources
theSmsParamsConfig.yml
## 配置短信
itmei-sms-config:
#是否开启短信发送
sms_send_or_not: true
# 对接的短信服务商
aliyun_sms:
accessKeyId: xxx
accessKeySecret: xxx
signName: xxx
#场景
sms_scene:
#场景类型
appointment_success: xxx
will_arrive_store: xxx
send_it_the_night_before: xxx
verification_code_sms: xxx
# 腾讯云服务商
tencent_sms:
accessKeyId: #你自己的accessKeyId
accessKeySecret: #你自己的accessKeySecret
signName: #你自己的签名
#场景
sms_scene:
#场景类型
appointment_success: #你自己的短信模板
will_arrive_store: #你自己的短信模板
send_it_the_night_before: #你自己的短信模板
verification_code_sms: #你自己的短信模板
Note:
The name of the connected service provider tencent_sms,aliyun_sms
is not random, each service provider corresponds to a service, and this service needs to implement SmsInterface
the interface
The completed Aliyun SMS implementation class:
Regarding 场景类型名称的key
the need to configure SmsSceneEnum
the definition in the enumeration class.
Because SmsParamsConfig.yml
the configuration data will be converted into JSONObject objects for use. In this way, in the subsequent code, we can use the corresponding configuration to call through the characteristics of the corresponding service provider as shown in the figure below.
SmsSupplierConfig
The class is used to obtain the yml file we defined and convert the data Map<String, Object>
into JSONObject
objects.
SMS Service Provider
Take the Alibaba Cloud SMS service that I have completed as an example:
create the corresponding SMS sending service class under the service package. If you SmsAliyunServiceImpl
need to implement SmsInterface
the interface rewriting method sendSms
and the sending method in it getDepartmentTemplateParam
, the corresponding service provider will provide sample code and the corresponding jar package or maven We can introduce the coordinates into the system, write the sample code sendSms
into the method and create a unique identifier in the service class and add it to SmsSupplierEnum
the SMS service provider enumeration
After writing the SMS service provider you are connected to according to the Alibaba Cloud sample code I wrote, we will start to assemble the required parameters and send the message.
Send SMS test
The test class is introduced in the project, and I wrote the code to send the test in the SmsApplicationTests class
@Test
void testSmsSend() {
//配置需要使用的短信服务商信息等
SmsTemplateConfig config = new SmsTemplateConfig();
config.setSceneEnum(SmsSceneEnum.APPOINTMENT_SUCCESS.getCode());
config.setSupplierEnum(SmsSupplierEnum.ALIYUN_SMS.getCode());
config.setSourceEnum(SmsSendSourceEnum.PC.getCode());
//自己短信模板的对应参数
Map<String, Object> templateParam = new HashMap<>();
templateParam.put("name", "itmei");
templateParam.put("date_time", DateUtil.now());
templateParam.put("address", "万达广场");
templateParam.put("customer_phone", "19999999999");
templateParam.put("landline", "wxid_itmei");
config.setTemplateParam(templateParam);
smsServiec.sendSms("19999999999", config, true);
}
Finish
The SMS is successfully sent and saved to the database.