Class OpenSms

java.lang.Object
cloud.opencode.base.sms.OpenSms

public final class OpenSms extends Object
Open SMS 开放短信

Main facade for SMS operations.

短信操作的主要门面。

Features | 主要功能:

  • Send SMS by phone number and content - 通过手机号和内容发送短信
  • Template-based SMS sending - 基于模板的短信发送
  • Multiple provider support (Aliyun, Tencent, Huawei, etc.) - 多服务商支持
  • Console mock mode for testing - 控制台模拟模式用于测试

Usage Examples | 使用示例:

// Create from config
OpenSms sms = OpenSms.of(SmsConfig.builder()
    .providerType(SmsProviderType.ALIYUN)
    .accessKey("key").secretKey("secret")
    .signName("MyApp").build());

// Send simple SMS
SmsResult result = sms.send("13800138000", "Your code is 1234");

// Console mock for testing
OpenSms consoleSms = OpenSms.console();

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • Null-safe: No (requires non-null provider) - 空值安全: 否(需要非空提供商)
Since:
JDK 25, opencode-base-sms V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static OpenSms of(SmsConfig config)
      Create from config 从配置创建
      Parameters:
      config - the config | 配置
      Returns:
      the OpenSms instance | OpenSms实例
    • of

      public static OpenSms of(SmsProvider provider)
      Create from provider 从提供商创建
      Parameters:
      provider - the provider | 提供商
      Returns:
      the OpenSms instance | OpenSms实例
    • console

      public static OpenSms console()
      Create console mock instance 创建控制台模拟实例
      Returns:
      the OpenSms instance | OpenSms实例
    • send

      public SmsResult send(String phoneNumber, String content)
      Send SMS 发送短信
      Parameters:
      phoneNumber - the phone number | 手机号码
      content - the content | 内容
      Returns:
      the result | 结果
    • send

      public SmsResult send(SmsMessage message)
      Send SMS message 发送短信消息
      Parameters:
      message - the message | 消息
      Returns:
      the result | 结果
    • sendBatch

      public List<SmsResult> sendBatch(List<SmsMessage> messages)
      Send batch SMS 批量发送短信
      Parameters:
      messages - the messages | 消息列表
      Returns:
      the results | 结果列表
    • sendToAll

      public List<SmsResult> sendToAll(List<String> phoneNumbers, String content)
      Send to multiple recipients 发送给多个收件人
      Parameters:
      phoneNumbers - the phone numbers | 手机号码列表
      content - the content | 内容
      Returns:
      the results | 结果列表
    • registerTemplate

      public OpenSms registerTemplate(String id, String content)
      Register template 注册模板
      Parameters:
      id - the template ID | 模板ID
      content - the template content | 模板内容
      Returns:
      this | 此对象
    • registerTemplate

      public OpenSms registerTemplate(SmsTemplate template)
      Register template 注册模板
      Parameters:
      template - the template | 模板
      Returns:
      this | 此对象
    • sendTemplate

      public SmsResult sendTemplate(String templateId, String phoneNumber, Map<String,String> variables)
      Send using template 使用模板发送
      Parameters:
      templateId - the template ID | 模板ID
      phoneNumber - the phone number | 手机号码
      variables - the variables | 变量
      Returns:
      the result | 结果
    • sendTemplateToAll

      public List<SmsResult> sendTemplateToAll(String templateId, List<String> phoneNumbers, Map<String,String> variables)
      Send template to multiple recipients 使用模板发送给多个收件人
      Parameters:
      templateId - the template ID | 模板ID
      phoneNumbers - the phone numbers | 手机号码列表
      variables - the variables | 变量
      Returns:
      the results | 结果列表
    • getProvider

      public SmsProvider getProvider()
      Get provider 获取提供商
      Returns:
      the provider | 提供商
    • getTemplateRegistry

      public SmsTemplateRegistry getTemplateRegistry()
      Get template registry 获取模板注册表
      Returns:
      the registry | 注册表
    • isAvailable

      public boolean isAvailable()
      Check if provider is available 检查提供商是否可用
      Returns:
      true if available | 如果可用返回true
    • close

      public void close()
      Close provider 关闭提供商