代码拉取完成,页面将自动刷新
Java 通信套件
是一个为企业级应用和开发者设计的多功能通信服务库,旨在通过Java应用程序简化短信和电子邮件的发送流程。本套件目前支持阿里云短信服务、腾讯短信服务以及SMTP电子邮件服务,未来将扩展支持更多服务商,以满足不断增长的市场需求。
在您的 pom.xml
文件中添加以下依赖项,以将 Java 通信套件
集成到您的项目中:
<dependencies>
<!-- 添加阿里云短信服务依赖 -->
<dependency>
<groupId>io.github.javpower</groupId>
<artifactId>sms-ali</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 添加腾讯短信服务依赖 -->
<dependency>
<groupId>io.github.javpower</groupId>
<artifactId>sms-tencent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 添加SMTP电子邮件服务依赖 -->
<dependency>
<groupId>io.github.javpower</groupId>
<artifactId>email-smtp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
以下是如何在Spring Boot应用程序中使用 Java 通信套件
的示例:
@Component
@Order(value = 1) // 指定执行顺序
public class TestRun implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
String tenant = "1号";
// 测试电子邮件发送
testEmail(tenant);
// 测试阿里云短信发送
testAli(tenant);
// 测试腾讯短信发送
testTen(tenant);
}
// 测试电子邮件发送方法
public void testEmail(String tenant) throws SendingException {
EmailConfiguration emailConfiguration = EmailConfiguration.builder()
.vendor(SmsVendor.SMTP_EMAIL.getVendorCode())
.username("xxxx")
.password("xxxxx")
.host("smtp.qq.com")
.port(465).build();
// 配置监听器以处理配置变更
EmailBuilder.addListener(new EmailConfigurationListener() {
@Override
public void onConfigurationChange(EmailConfiguration newConfig) {
// 处理电子邮件配置变更...
}
});
// 使用构建器发送邮件
EmailBuilder.factory(tenant, emailConfiguration)
.subject("测试邮件主题")
.to("recipient@example.com")
.param("code", "123456")
.onSend(response -> {
if (response.isSuccess()) {
System.out.println("邮件发送成功。");
} else {
System.out.println("邮件发送失败:" + response.getMessage());
}
})
.send();
}
// 测试阿里云短信发送方法
public void testAli(String tenant) throws SendingException {
// 配置和发送逻辑...
SmsConfiguration aliConfig = SmsConfiguration.builder().
apiKey("hello").
apiSecret("world").
vendor(SmsVendor.ALIYUN.getVendorCode()).build();
SmsBuilder.addListener(new SmsConfigurationListener() {
@Override
public void onConfigurationChange(SmsConfiguration newConfig) {
// 处理配置变化,如重新初始化服务等
System.out.println(tenant+"配置变化了:"+newConfig.toString());
}
});
SmsBuilder.factory(tenant,aliConfig)
.signName("xxxxx")
.to("13988888888")
.templateId("template123")
.param("name", "John")
.onSend(response -> {
if (response.isSuccess()) {
System.out.println("SMS sent successfully."+response.getMessage());
} else {
System.out.println("Failed to send SMS: " + response.getMessage());
}
})
.send();
}
// 测试腾讯短信发送方法
public void testTen(String tenant) throws SendingException {
// 配置和发送逻辑...
SmsConfiguration tenConfig = SmsConfiguration.builder().
apiKey("hello").
apiSecret("world").
vendor(SmsVendor.TENCENT.getVendorCode()).build();
SmsBuilder.addListener(new SmsConfigurationListener() {
@Override
public void onConfigurationChange(SmsConfiguration newConfig) {
// 处理配置变化,如重新初始化服务等
System.out.println(tenant+"配置变化了:"+newConfig.toString());
}
});
SmsBuilder.factory(tenant,tenConfig)
.signName("xxxxx")
.appId("xxxx")
.senderid("")//国际专用
.to("13988888888")
.templateId("template123")
.param("name", "John")
.onSend(response -> {
if (response.isSuccess()) {
System.out.println("SMS sent successfully."+response.getMessage());
} else {
System.out.println("Failed to send SMS: " + response.getMessage());
}
})
.send();
}
}
我们欢迎任何形式的贡献,包括但不限于:
本项目遵循 Apache License 2.0。
感谢所有开源贡献者和社区成员的支持。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。