Mastering Elegant SMS Sending in Java: Client vs Server Modes
This guide explains how Java applications can send SMS messages efficiently by comparing client‑side SDK integration with a dedicated server‑side SMS platform, covering provider choices, code examples, multi‑channel aggregation with SMS4J, and design considerations for high‑availability messaging.
1 Client/Server Modes
Java applications send SMS through providers' APIs such as Alibaba Cloud, Tencent Cloud, Huawei Cloud, or YiMei. Two main approaches exist:
1. Client mode – the application calls the provider's API directly.
2. Server mode – a separate SMS platform service is created, and the application uses its SDK.
2 Client Mode
1. Use third‑party SDK
Most providers offer ready‑made SDKs; adding the SDK dependency and configuration enables simple SMS sending. Example using Alibaba Cloud SMS:
@RestController
@RequestMapping("/test/")
public class DemoController {
// Test sending a fixed‑template SMS
@RequestMapping("/")
public void doLogin(String username, String password) {
// Alibaba Cloud sends SMS
SmsFactory.createSmsBlend(SupplierType.ALIBABA)
.sendMessage("18888888888", "123456");
// Huawei sends SMS
SmsFactory.createSmsBlend(SupplierType.HUAWEI)
.sendMessage("16666666666", "000000");
}
}While straightforward, using multiple provider SDKs can lead to inconsistent code and dependency conflicts.
2. Aggregate multiple providers
To avoid duplication, the open‑source SMS4J framework aggregates several SMS services. Integration steps in a SpringBoot project:
Add Maven dependency:
<dependency>
<groupId>org.dromara.sms4j</groupId>
<artifactId>sms4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>Configure providers in application.yml (example shows Alibaba and Huawei settings).
sms:
alibaba:
accessKeyId: YOUR_ACCESS_KEY
accessKeySecret: YOUR_SECRET
signature: TestSignature
templateId: SMS_215125134
templateName: code
requestUrl: dysmsapi.aliyuncs.com
huawei:
appKey: 5N6fvXXXX920HaWhVXXXXXX7fYa
app-secret: Wujt7EYzZTBXXXXXXEhSP6XXXX
signature: HuaweiTest
sender: 8823040504797
template-id: acXXXXXXXXc274b2a8263479b954c1ab5Use the unified API in code:
@RestController
@RequestMapping("/test/")
public class DemoController {
@RequestMapping("/")
public void send() {
SmsFactory.createSmsBlend(SupplierType.ALIBABA)
.sendMessage("18888888888", "123456");
}
}Client mode is simple but can become messy when many providers are involved.
3 Server Mode
Server mode builds an independent SMS platform service that applications call via its SDK. Key design points include:
Application management – each app gets an appKey and appSecret with isolated rate‑limiting.
Compact SDK offering template‑based single or batch sending, e.g.:
public SmsSenderResult sendSmsByTemplateId(
String mobile,
String templateId,
Map<String, String> templateParam);Centralized signature and template management, allowing one template to bind multiple channels for high availability.
Multi‑channel adaptation – load provider SDKs via SPI to avoid dependency conflicts.
Extensible features such as batch sending, delayed delivery, routing strategies, and flexible rate limiting.
An example architecture includes a simple SDK‑style API, a message queue, worker services that balance load across channels, and a dashboard for configuration and monitoring.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
