亲宝软件园·资讯

展开

Spring Boot邮箱发送

it永远的学习者 人气:0

1. 准备工作

登录进自己的 qq 邮箱获取授权码

设置 – 账户 – POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务

image-20220218112624617

点击开始获取授权码,将授权码保存下来,下面代码整合的时候需要使用

image-20220218112729768

2. 代码整合

创建一个 Spring Boot 项目,引入以下依赖

<!-- email -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

配置文件

server:
  port: 8080
spring:
  # email 配置
  mail:
    # SMTP 服务器地址
    host: smtp.qq.com
    username: # 你的qq 邮箱地址
    password: # 授权码,上面从 qq 邮箱官网得到的授权码
    port: 587
    protocol: smtp
    default-encoding: UTF-8

创建一个邮件收件人信息的实体类

/**
 * @author lishisen
 * @description 邮件收件人信息
 * @date 2022/1/9 9:37
 **/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class EmailModel implements Serializable {

    // 收件人邮箱地址
    private String receiveEmailAddress;
    // 邮箱主题
    private String emailTheme;
    // 邮件内容
    private String emailContent;

}

创建一个服务提供类,SendEmailService.java

/**
 * @author lishisen
 * @description 邮件发送服务类
 * @date 2022/1/9 9:40
 **/
@Slf4j
@Component
public class SendEmailService {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private JavaMailSenderImpl javaMailSenderImpl;

    // 发送者地址
    private String posterAddress = "1876419736@qq.com";
    // 发送者名称
    private String posterName = "测试邮件发送";

    /**
     * 文本发送
     **/
    public void sendEmailAsText(final EmailModel emailModel) {
        System.out.println(emailModel.toString());
        MimeMessagePreparator mimeMessagePreparator = mimeMessage -> {
            mimeMessage.setFrom(posterAddress);
            mimeMessage.setRecipients(Message.RecipientType.TO, emailModel.getReceiveEmailAddress());
            mimeMessage.setText("发送者地址:" + posterAddress + '\n'
                    + "发送者名称:" + posterName + '\n'
                    + "内容:" + emailModel.getEmailContent());
        };
        try {
            this.javaMailSender.send(mimeMessagePreparator);
            log.info("邮箱已返送至[{}]邮箱!", emailModel.getReceiveEmailAddress());
        } catch (MailException e) {
            log.error("邮箱异常:{}", e);
        }
    }

    /**
     * html 网页发送
     * 该方法为同步方法
     **/
    public void sendEmailAsSysExceptionHtml(final EmailModel emailModel, String info) {
        MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
        MimeMessageHelper mimeMessageHelper;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(posterAddress);
            mimeMessageHelper.setTo(emailModel.getReceiveEmailAddress());
            mimeMessageHelper.setSubject(emailModel.getEmailTheme());
            mimeMessageHelper.setText(
                    "<!DOCTYPE html>\n" +
                            "<html lang=\"en\">\n" +
                            "<head>\n" +
                            "    <meta charset=\"UTF-8\">\n" +
                            "    <title>Title</title>\n" +
                            "</head>\n" +
                            "<body>\n" +
                            "<h2>" + info + "</h2>\n" +
                            "<h3>您的验证码为:<span style='color: red'" + ">" +
                            emailModel.getEmailContent() +
                            "</span> " + ",请在 5 分钟内进行验证</h3>\n" +
                            "</body>\n" +
                            "</html>"
                    , true);
            this.javaMailSender.send(mimeMessage);
            log.info("邮箱已返送至[{}]邮箱!", emailModel.getReceiveEmailAddress());
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

测试使用

@Autowired
private SendEmailService sendEmailService;
@Test
public void sendMailTest() {
    // 创建收件人实体类,封装收件人的信息
    EmailModel emailModel = new EmailModel();
    emailModel.setReceiveEmailAddress("18xxxxxqq.com");
    emailModel.setEmailTheme("测试发送");
    emailModel.setEmailContent("测试发送邮箱主体信息内容");
    // 调用发送服务方法执行发送任务
    sendEmailService.sendEmailAsText(emailModel);
    System.out.println("邮件发送成功");
}

image-20220218113229755

总结

加载全部内容

相关教程
猜你喜欢
用户评论