动态邮件发送

  • Post author:
  • Post category:其他


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @author :
 * @date :
 * description: 邮件发送参数
 * modified By:
 * version:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SendEmailParam implements Serializable {


    private static final long serialVersionUID = -406457693781112640L;

    /**
     * 邮箱配置id
     */
    @NotNull(message = "邮箱id不能为空")
    private Long emilId;

    /**
     * 邮件收件人
     */
    @NotBlank(message = "邮件收件人不能为空")
    private String to;

    /**
     * 邮件抄送人,多个使用,分割
     */
    private String cc;

    /**
     * 邮件标题
     */
    @NotBlank(message = "邮件标题不能为空")
    private String title;

    /**
     * 邮件内容
     */
    @NotBlank(message = "邮件内容不能为空")
    private String message;
}

  /**
     * 简单的动态邮件发送
     *
     * @param param 邮件发送参数
     * @return 是否发送成功
     */
    @Override
    public ResponseMessageDTO<Boolean> sendTextEmail(SendEmailParam param) {
        EmailDO emailConfigInfo = getById(param.getEmilId());
        if (Objects.isNull(emailConfigInfo)) {
            return ResponseMessageDTO
                    .<Boolean>builder()
                    .code(ErrorCodeEnum.VALIDATED_ERROR.getCode())
                    .message("邮箱配置不存在")
                    .success(Boolean.FALSE)
                    .build();
        }
        try {
            // 生成 发送sender
            JavaMailSenderImpl sender = new JavaMailSenderImpl();
            sender.setHost(emailConfigInfo.getHost());
            sender.setDefaultEncoding(Constants.DEFAULT_CODE);
            sender.setUsername(emailConfigInfo.getUserName());
            sender.setPassword(emailConfigInfo.getAuthorizationCode());
            Properties p = new Properties();
            p.setProperty("mail.smtp.auth", "true");
            sender.setJavaMailProperties(p);

            // 发送邮件
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setTo(param.getTo());
            mailMessage.setFrom(emailConfigInfo.getUserName());
            if (StringUtils.isNotBlank(param.getCc())) {
                mailMessage.setCc(param.getCc().split(","));
            }
            mailMessage.setSubject(param.getTitle());
            mailMessage.setText(param.getMessage());
            sender.send(mailMessage);
            return ResponseMessageDTO.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("文本邮件发送失败", e);
            return ResponseMessageDTO
                    .   <Boolean>builder()
                    .code(ErrorCodeEnum.SEND_EMAIL_ERROR.getCode())
                    .message(ErrorCodeEnum.SEND_EMAIL_ERROR.getDesc())
                    .success(Boolean.FALSE)
                    .build();
        }
    }
@SpringBootTest
@Slf4j
public class EmailSendTest {

    @Resource
    private EmailService emailService;


    @Test
    void sendMail() {
        emailService.sendTextEmail(SendEmailParam
                .builder()
                .to("x'x'x'x'x@qq.com")
                .cc("x'x'x'x'x@163.com,x'x'x'x@163.com")
                .emilId(2L)
                .title("测试邮件标题")
                .message("测试邮件 发送时间:" + DateUtils.getDateString(new Date(), DateUtils.DATETIME_PATTERN))
                .build());
    }
}



版权声明:本文为qq_45464015原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。