content: 
  package com.platform.service.impl;
  
  import cn.hutool.extra.mail.Mail;
  import cn.hutool.extra.mail.MailAccount;
  import lombok.RequiredArgsConstructor;
  import com.platform.domain.EmailConfig;
  import com.platform.domain.vo.EmailVo;
  import com.platform.exception.BadRequestException;
  import com.platform.repository.EmailRepository;
  import com.platform.service.EmailService;
  import com.platform.utils.EncryptUtils;
  import org.springframework.cache.annotation.CacheConfig;
  import org.springframework.cache.annotation.CachePut;
  import org.springframework.cache.annotation.Cacheable;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  import java.util.Optional;
  
  /**
   * @author AllDataDC
   * @date 2023-01-27
   */
  @Service
  @RequiredArgsConstructor
  @CacheConfig(cacheNames = "email")
  public class EmailServiceImpl implements EmailService {
  
      private final EmailRepository emailRepository;
  
      @Override
      @CachePut(key = "'config'")
      @Transactional(rollbackFor = Exception.class)
      public EmailConfig config(EmailConfig emailConfig, EmailConfig old) throws Exception {
          emailConfig.setId(1L);
          if(!emailConfig.getPass().equals(old.getPass())){
              // 对称加密
              emailConfig.setPass(EncryptUtils.desEncrypt(emailConfig.getPass()));
          }
          return emailRepository.save(emailConfig);
      }
  
      @Override
      @Cacheable(key = "'config'")
      public EmailConfig find() {
          Optional<EmailConfig> emailConfig = emailRepository.findById(1L);
          return emailConfig.orElseGet(EmailConfig::new);
      }
  
      @Override
      @Transactional(rollbackFor = Exception.class)
      public void send(EmailVo emailVo, EmailConfig emailConfig){
          if(emailConfig.getId() == null){
              throw new BadRequestException("请先配置，再操作");
          }
          // 封装
          MailAccount account = new MailAccount();
          // 设置用户
          String user = emailConfig.getFromUser().split("@")[0];
          account.setUser(user);
          account.setHost(emailConfig.getHost());
          account.setPort(Integer.parseInt(emailConfig.getPort()));
          account.setAuth(true);
          try {
              // 对称解密
              account.setPass(EncryptUtils.desDecrypt(emailConfig.getPass()));
          } catch (Exception e) {
              throw new BadRequestException(e.getMessage());
          }
          account.setFrom(emailConfig.getUser()+"<"+emailConfig.getFromUser()+">");
          // ssl方式发送
          account.setSslEnable(true);
          // 使用STARTTLS安全连接
          account.setStarttlsEnable(true);
          String content = emailVo.getContent();
          // 发送
          try {
              int size = emailVo.getTos().size();
              Mail.create(account)
                      .setTos(emailVo.getTos().toArray(new String[size]))
                      .setTitle(emailVo.getSubject())
                      .setContent(content)
                      .setHtml(true)
                      //关闭session
                      .setUseGlobalSession(false)
                      .send();
          }catch (Exception e){
              throw new BadRequestException(e.getMessage());
          }
      }
  }
  