SMTP邮件服务在线配置化

v1.4.1
hiparker 4 years ago
parent 01de876b14
commit 12d8010af6

@ -0,0 +1,62 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.modulars.tools.email.enums;
/**
* Email
*
* @author Parker
*/
public enum EmailType {
/** SMTP 服务 */
EMAIL_SMTP("email_smtp", "SMTP地址"),
EMAIL_PORT("email_port", "SMTP端口"),
EMAIL_SSL_ENABLE("email_ssl_enable", "开启SSL认证"),
EMAIL_ACCOUNT("email_account", "邮箱账号"),
EMAIL_PASSWORD("email_password", "邮箱账号密码"),
EMAIL_ADDRESSER("email_addresser", "发件人"),
;
private final String code;
private final String desc;
public static EmailType getType(String cacheType) {
EmailType[] var1 = values();
for (EmailType type : var1) {
if (type.code.equalsIgnoreCase(cacheType)) {
return type;
}
}
return null;
}
public String getCode() {
return this.code;
}
public String getDesc() {
return this.desc;
}
// =================
EmailType(final String code, final String desc) {
this.code = code;
this.desc = desc;
}
}

@ -0,0 +1,103 @@
package org.opsli.modulars.tools.email.service.impl;
import cn.hutool.core.convert.Convert;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.common.enums.DictType;
import org.opsli.core.utils.OptionsUtil;
import org.opsli.core.utils.ValidationUtil;
import org.opsli.modulars.tools.email.enums.EmailType;
import org.opsli.modulars.tools.email.service.IEmailService;
import org.opsli.modulars.tools.email.wrapper.EmailModel;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Collections;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.tools.email.service
* @Author: Parker
* @CreateTime: 2020-09-16 17:34
* @Description:
*/
@Service
public class EmailServiceImpl implements IEmailService {
@Override
public String send(String to, String subject, String content) {
// 发送邮件
return this.send(Collections.singletonList(to), subject ,content);
}
@Override
public String send(Collection<String> tos, String subject, String content) {
// 发送邮件
return this.send(tos, subject ,content, false);
}
@Override
public String send(String to, String subject, String content, boolean isHtml) {
// 发送邮件
return this.send(Collections.singletonList(to), subject ,content, isHtml);
}
@Override
public String send(Collection<String> tos, String subject, String content, boolean isHtml) {
// 校验发送邮件数据是否正确
for (String to : tos) {
EmailModel emailModel = new EmailModel();
emailModel.setTo(to);
emailModel.setSubject(subject);
emailModel.setContent(content);
ValidationUtil.verify(emailModel);
}
// 获得配置信息
MailAccount mailAccount = this.getMailAccount();
// 发送邮件
return MailUtil.send(mailAccount, tos , subject ,content, isHtml);
}
/**
*
* @return MailAccount
*/
private MailAccount getMailAccount(){
// 获得配置数据
OptionsModel smtp = OptionsUtil.getOptionByCode(EmailType.EMAIL_SMTP.getCode());
OptionsModel port = OptionsUtil.getOptionByCode(EmailType.EMAIL_PORT.getCode());
OptionsModel sslEnable = OptionsUtil.getOptionByCode(EmailType.EMAIL_SSL_ENABLE.getCode());
OptionsModel account = OptionsUtil.getOptionByCode(EmailType.EMAIL_ACCOUNT.getCode());
OptionsModel password = OptionsUtil.getOptionByCode(EmailType.EMAIL_PASSWORD.getCode());
OptionsModel addresser = OptionsUtil.getOptionByCode(EmailType.EMAIL_ADDRESSER.getCode());
MailAccount mailAccount = new MailAccount();
mailAccount.setAuth(true);
if(smtp != null){
mailAccount.setHost(smtp.getOptionValue());
}
if(port != null){
mailAccount.setPort(Convert.toInt(port.getOptionValue()));
}
if(sslEnable != null){
mailAccount.setSslEnable(
DictType.NO_YES_YES.getValue().equals(sslEnable.getOptionValue()));
}
if(account != null){
mailAccount.setUser(account.getOptionValue());
}
if(password != null){
mailAccount.setPass(password.getOptionValue());
}
if(addresser != null){
mailAccount.setFrom(addresser.getOptionValue());
}
return mailAccount;
}
}

@ -0,0 +1,47 @@
package org.opsli.modulars.tools.email.web;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.base.result.ResultVo;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.modulars.tools.email.service.IEmailService;
import org.opsli.modulars.tools.email.wrapper.EmailModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.tools.email.web
* @Author: Parker
* @CreateTime: 2020-09-13 17:40
* @Description: Controller
*/
@Api(tags = "邮件类")
@Slf4j
@ApiRestController("/email")
public class EmailRestController {
@Autowired
private IEmailService iEmailService;
/**
*
* @param model
* @return ResultVo
*/
@ApiOperation(value = "测试发送邮件", notes = "测试发送邮件")
@PostMapping("/testSend")
public ResultVo<?> testSend(EmailModel model) {
try {
String result = iEmailService
.send(model.getTo(), model.getSubject(), model.getContent());
return ResultVo.success(result);
}catch (Exception e){
return ResultVo.error("邮件发送失败 - " + e.getMessage());
}
}
}

@ -0,0 +1,40 @@
package org.opsli.modulars.tools.email.wrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.opsli.common.annotation.validation.ValidationArgs;
import org.opsli.common.annotation.validation.ValidationArgsLenMax;
import org.opsli.common.enums.ValiArgsType;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.test.entity
* @Author: Parker
* @CreateTime: 2020-09-16 17:33
* @Description:
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class EmailModel {
/** 收件人 */
@ApiModelProperty(value = "收件人")
@ValidationArgs({ValiArgsType.IS_NOT_NULL})
@ValidationArgsLenMax(200)
private String to;
/** 主题 */
@ApiModelProperty(value = "主题")
@ValidationArgs({ValiArgsType.IS_NOT_NULL})
@ValidationArgsLenMax(200)
private String subject;
/** 内容 */
@ApiModelProperty(value = "内容")
@ValidationArgsLenMax(20000)
private String content;
}
Loading…
Cancel
Save