springboot集成qq邮箱

1.maven依赖

 <!-- email依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
package com.atguigu.yygh.cmn.controller;

import lombok.Data;

import java.io.Serializable;
@Data
public class MailBean implements Serializable {
    private static final long serialVersionUID = -2116367492649751914L;
    private String recipient;//邮件接收人
    private String subject; //邮件主题
    private String content; //邮件内容
    // 省略setget方法
}
package com.atguigu.yygh.cmn.controller;


import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailUtil {
    @Value("${spring.mail.username}")
    private String MAIL_SENDER; //邮件发送者

    @Autowired
    private JavaMailSender javaMailSender;

    private Logger logger = LoggerFactory.getLogger(MailUtil.class);

    /**
     * 发送文本邮件
     *
     * @param mailBean
     */
    public  void sendSimpleMail(MailBean mailBean) {
        try {
            SimpleMailMessage mailMessage= new SimpleMailMessage();
            mailMessage.setFrom(MAIL_SENDER);
            mailMessage.setTo(mailBean.getRecipient());
            mailMessage.setSubject(mailBean.getSubject());
            mailMessage.setText(mailBean.getContent());
            //mailMessage.copyTo(copyTo);

            javaMailSender.send(mailMessage);
        } catch (Exception e) {

        }
    }
}

配置文件

spring.mail.host=smtp.qq.com
# 邮箱地址
spring.mail.username=906533692@qq.com
# 邮箱授权码
spring.mail.password=ymfxhjieunuzbdgg
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8

测试类

package com.atguigu.yygh.cmn;

import com.alibaba.excel.util.DateUtils;
import com.atguigu.yygh.cmn.controller.MailBean;
import com.atguigu.yygh.cmn.controller.MailUtil;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMailApplicationTests {

    @Autowired
    private MailUtil mailUtil;



    //接收人
    private static final String RECIPINET = "3330397915a@qq.com";

    /**
     * 发送文本邮件
     */
    @Test
    public void sendSimpleMail() {
        MailBean mailBean = new MailBean();
        mailBean.setRecipient(RECIPINET);
        mailBean.setSubject("SpringBootMail之这是一封文本的邮件");
        mailBean.setContent("SpringBootMail发送一个简单格式的邮件666666,时间为:" + DateUtils.format(new Date()));

        mailUtil.sendSimpleMail(mailBean);
    }

}

参考地址

SpringBoot发送邮件,QQ邮箱_springboot 发送qq邮件_军大君的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
你可以使用Spring Boot集成JavaMail来发送电子邮件,包括QQ邮箱。首先,你需要在Spring Boot项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 然后,在你的application.properties(或application.yml)文件中配置QQ邮箱的SMTP服务器信息: ```properties spring.mail.host=smtp.qq.com spring.mail.port=587 spring[email protected] spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 请确保将 "[email protected]"和"your-email-password" 替换为你自己的QQ邮箱地址和密码。 接下来,你可以创建一个包含邮件发送逻辑的Service类。下面是一个简单的示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { private JavaMailSender javaMailSender; @Autowired public EmailService(JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; } public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); javaMailSender.send(message); } } ``` 在你的Controller或其他地方使用这个Service类来发送邮件。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { private EmailService emailService; @Autowired public EmailController(EmailService emailService) { this.emailService = emailService; } @GetMapping("/sendEmail") public String sendEmail() { String to = "[email protected]"; String subject = "Test Email"; String text = "This is a test email from Spring Boot."; emailService.sendEmail(to, subject, text); return "Email sent successfully!"; } } ``` 记得将 "[email protected]" 替换为你要发送邮件的收件人地址。 这样,当你访问 "/sendEmail" 路径时,就会触发邮件发送。确保你的应用程序能够连接到互联网以及QQ邮箱的SMTP服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值