SpringBoot整合QQ邮箱,实现邮箱发送功能。

标题

  • 1.开启邮箱服的POP3/IMAP/SMTP 服务
  • 2.导入工具包
  • 3.代码编写

1.开启邮箱服的POP3/IMAP/SMTP 服务

1.打开网页QQ邮箱,找到设置---->账户—>服务,开启服务。
注意:要用QQ登录的邮箱,微信登录邮箱会出问题

2.导入工具包

    
     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-mailartifactId>
     dependency>

3.代码编写

在application.yml配置信息

Email:
  host: smtp.qq.com
  username: 填你的邮箱
  password: 填授权码
  port: 587
 

编写一个service类

package com.hmdp.service.impl;

import com.hmdp.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Objects;
import java.util.Properties;

/**
 * @author : by smomo
 * @date : 2023/6/13 19:43
 * @description :
 */


@Slf4j
@Service
public class EmailServiceImpl implements EmailService {


    private final  JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

    @Value("${Email.host}")
    private String email_host;
    @Value("${Email.username}")
    private String email_username;
    @Value("${Email.password}")
    private String email_password;

    @Value("${Email.port}")
    private int email_port;


    /**
     * 初始化javaMailSender
     */
    @PostConstruct
    public void init()
    {
        //
        javaMailSender.setHost(email_host);
        javaMailSender.setUsername(email_username);
        javaMailSender.setPassword(email_password);
        javaMailSender.setPort(email_port);
        javaMailSender.setDefaultEncoding("UTF-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.debug", "true");
        javaMailSender.setJavaMailProperties(p);
    }

    private void send(String email, String template) throws MessagingException {

        //创建一个MINE消息
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mineHelper = new MimeMessageHelper(mimeMessage, true);
        // 设置邮件主题
        mineHelper.setSubject("Smomo博客");
        // 设置邮件发送者
        mineHelper.setFrom(Objects.requireNonNull(javaMailSender.getUsername()));
        // 设置邮件接收者,可以有多个接收者,中间用逗号隔开
        mineHelper.setTo(email);
        // 设置邮件发送日期
        mineHelper.setSentDate(new Date());
        // 设置邮件的正文
        mineHelper.setText(template,true);
        // 发送邮件
        javaMailSender.send(mimeMessage);
}


    @Override
    public void sendCode(String email) throws MessagingException {
        //随机数
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        String content = "\n" +
                "\t
\n" + "
\n" + "
\n" + " \n" + "
\n" + " \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+" \n"+"
\n" + " " + " 拾壹博客. \n" + "
\n" + "

验证您的邮箱地址

\n"
+ "

感谢您使用 smomo博客.

\n"
+ "

以下是您的邮箱验证码,请将它输入到 smomo博客 的邮箱验证码输入框中:

\n"
+ "
\n" + "

"+code+"

\n"
+ "
\n" + "

这一封邮件包括一些您的私密的 拾壹博客 账号信息,请不要回复或转发它,以免带来不必要的信息泄露风险。

\n"
+ "
\n" + "
\n"
+ "

shiyi - smomo博客

\n"
+ "
\n"
+ "
\n"
+ " \n" + "
\n"
+ "
\n"
+ " \n" + "
\n"
+ "\n"; send(email,content); log.info("邮箱验证码发送成功,邮箱:{},验证码:{}",email,code); } }

新建controller类,进行测试

@RestController
public class SendCodeController {

    @Autowired
    private EmailService emailService;

    @GetMapping("/sendCode")
    public String sendCode(String email) throws MessagingException {
        System.out.println("11111");
        emailService.sendCode(email);
        return "发送成功";
    }
}

发送请求,进行测试

SpringBoot整合QQ邮箱,实现邮箱发送功能。_第1张图片

功能成功实现

你可能感兴趣的:(spring,boot,java,spring,后端)