1.邮件发送
1.1 邮件服务设置
1.1.1 方法一:通过yaml文件配置
pamirs:
email:
smtp:
smtpHost: smtp.exmail.qq.com
smtpUser: xxx@xxx.com
smtpPassword: xxxxxx
smtpPort: 465
smtpSecurity: SSL
#邮件模块可后续后台自行添加
templates:
- name: 邮箱注册邮件
title: '${code}是你此次注册的验证码'
body: '<div>Hi ${realname},</div><div>你正在使用验证码注册。</div>'
1.1.2 方法二:工程启动加入初始化设置方法
/**
* 初始化邮件模板
*/
private void initEmailTemplate(){
EmailSenderSource emailSenderSource = new EmailSenderSource();
emailSenderSource.setName("邮件发送服务");
emailSenderSource.setType(MessageEngineTypeEnum.EMAIL_SEND);
//优先级
emailSenderSource.setSequence(10);
//发送账号
emailSenderSource.setSmtpUser("xxx@xxx.com");
//发送密码
emailSenderSource.setSmtpPassword("xxxxxx");
//发送服务器地址和端口
emailSenderSource.setSmtpHost("smtp.exmail.qq.com");
emailSenderSource.setSmtpPort(465);
//" None: SMTP 对话用明文完成。" +
//" TLS (STARTTLS): SMTP对话的开始时要求TLS 加密 (建议)" +
//" SSL/TLS: SMTP对话通过专用端口用 SSL/TLS 加密 (默认是: 465)")
emailSenderSource.setSmtpSecurity(EmailSendSecurityEnum.SSL);
emailSenderSource.setActive(true);
emailSenderSource.createOrUpdate();
List<EmailTemplate> templates = new ArrayList<>();
templates.add(new EmailTemplate().setName("重置密码邮件模板").setTitle("请确认你的密码修改请求").setBody("<div>Hi ${realname},</div><div>你正在使用验证码注册。</div>").setModel(PamirsUser.MODEL_MODEL).setEmailSenderSource(emailSenderSource));
new EmailTemplate().createOrUpdateBatch(templates);
}
1.2 调用邮件发送组件发送邮件
/**
* 代码中使用消息组件发送Email
*/
public void sendEmailByTemplate(){
try {
EmailSender emailSender = (EmailSender) MessageEngine.get(MessageEngineTypeEnum.EMAIL_SEND).get(null);;
EmailTemplate template = new EmailTemplate().setName("邮件模版名称").queryOne();
//标题:${name}
//内容:${fieldInt}次数
String sendTo = "xxx@xxx.com";
String copyTo = "yyy@yyy.com";
Map<String, Object> objectData = new HashMap<>();
objectData.put("name","张三");
objectData.put("fieldInt",999);
Boolean aBoolean = emailSender.send(template, objectData, sendTo, copyTo);
if (null == aBoolean || !aBoolean) {
log.error("发送邮件失败");
}
} catch (Exception e) {
log.error("发送确认邮件失败:,异常:{}", e);
}
}
2.发送短信
2.1 短信通道设置
2.1.1 方法一:通过yaml文件配置
pamirs:
sms:
aliyun:
signatureMethod: HMAC-SHA1
endpoint: https://dysmsapi.aliyuncs.com
version: '2017-05-25'
accessKeyId: xxxxxxxxxxxxx
signatureVersion: '1.0'
accessKeySecret: xxxxxxxxxxxx
regionId: cn-hangzhou
timeZone: GMT
signName: xxxxxx
2.1.2 方法二:工程启动加入初始化设置方法
private void initSmsConfig() {
SmsChannelConfig smsChannelConfig = new SmsChannelConfig();
SmsChannelConfig smsChannelConfigs = smsChannelConfig.setChannel(SMSChannelEnum.ALIYUN)
.setAccessKeyId("xxxxxx")
.setSignName("xxxxxx")
.queryOne();
if (null == smsChannelConfigs) {
smsChannelConfig.setChannel(SMSChannelEnum.ALIYUN);
smsChannelConfig.setType(MessageEngineTypeEnum.SMS_SEND);
smsChannelConfig.setSignatureMethod("HMAC-SHA1");
smsChannelConfig.setEndpoint("https://dysmsapi.aliyuncs.com");
smsChannelConfig.setVersion("2017-05-25");
smsChannelConfig.setAccessKeyId("xxxxxx");
smsChannelConfig.setAccessKeySecret("xxxxxx");
smsChannelConfig.setSignatureVersion("1.0");
smsChannelConfig.setRegionId("cn-hangzhou");
smsChannelConfig.setTimeZone("GMT");
smsChannelConfig.setSignName("xxxxxx");
smsChannelConfig.setActive(Boolean.TRUE);
smsChannelConfig.create();
} else {
smsChannelConfigs.updateById();
}
//初始化短信模版,需要先到阿里云申请短信模版
SmsTemplate smsTemplate = new SmsTemplate();
smsTemplate.setChannel(SMSChannelEnum.ALIYUN);
smsTemplate.setTemplateType(SMSTemplateTypeEnum.SIGN_IN);
smsTemplate.setTemplateCode("SMS_252710149");//阿里云短信模版的Code
smsTemplate.setTemplateContent("验证码: ${code},您正在登录。10分钟内有效,请注意保密,请勿向他人泄露您的验证码。");//阿里云短信模版
List<SmsTemplate> smsTemplates = new ArrayList<>();
smsTemplates.add(smsTemplate);
new SmsTemplate().createOrUpdateBatch(smsTemplates);
}
2.2 调用邮件发送组件发送邮件
public void sendSms(){
try {
List<String> receivePhoneNumbers = new ArrayList<>();
receivePhoneNumbers.add("13800000000");
// 占位符处理
Map<String, String> vars = new HashMap<>();
vars.put("code", "123456");
SmsTemplate smsTemplate = new SmsTemplate().setChannel(SMSChannelEnum.ALIYUN);
smsTemplate.setTemplateType(SMSTemplateTypeEnum.SIGN_IN);
smsTemplate.setTemplateCode("SMS_252710149").queryOne();
SMSSender smsSender = (SMSSender) MessageEngine.get(MessageEngineTypeEnum.SMS_SEND).get(null);
receivePhoneNumbers.stream().distinct().forEach(it -> {
try {
if (!smsSender.smsSend(smsTemplate, it, vars)) {
//todo 错误处理
}
} catch (Exception e) {
//todo 错误处理
}
});
} catch (Exception e) {
//todo 错误处理
}
}
Oinone社区 作者:数式-海波原创文章,如若转载,请注明出处:https://doc.oinone.top/backend/4346.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验