SpringBoot发送QQ邮件

1.新建springboot项目

2.导入依赖

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

3.配置

# 作为发送者的邮箱
spring.mail.username=2515323344@qq.com
# 密码是一个授权码在自己的qq邮箱中获取 
spring.mail.password=chvhjzenctjrdhgh
spring.mail.host=smtp.qq.com
# 开启加密验证qq独有
spring.mail.properties.mail.smtp.ssl.enable=true;
3.1

授权码获取方式
1.登录qq邮箱点击设置
在这里插入图片描述
2.点击账户在这里插入图片描述
3.开启pop/stmp服务
在这里插入图片描述
5.生成授权码
在这里插入图片描述

4.测试发送

package com.wh;

import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class GgshopApplicationTests {

    @Autowired
    JavaMailSender mailSender;


    //简单邮件

    @Test
    void contextLoads() {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setSubject("你好呀");//发送的主题
        mailMessage.setText("谢谢你啊");//发送的类容
        mailMessage.setTo("2515323344@qq.com");//接收的人的邮箱
        mailMessage.setFrom("2515323344@qq.com");//发送者
        mailSender.send(mailMessage);
    }
    //复杂邮件
    @Test
    void contextLoads2() throws MessagingException {
        MimeMessage mimeMessage =mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("nihao++");
        helper.setText("<p style='color:red'>谢谢你啊啊实打实</p>",true);
        helper.setTo("2515323344@qq.com");
        helper.setFrom("2515323344@qq.com");
        helper.addAttachment("logo.jpg",new File("C:\\Users\\Desktop\\logo.png"));//添加附件
        mailSender.send(mimeMessage);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中使用QQ邮件发送,您需要以下步骤: 1. 在QQ邮箱中开启SMTP服务,并获取授权码。登录QQ邮箱,在“设置”中找到“账户”选项卡,点击“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”,开启SMTP服务并获取授权码。 2. 在您的Spring Boot项目中添加JavaMail依赖。您可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> ``` 3. 在application.properties文件中添加SMTP服务器信息和授权码。在application.properties文件中,添加以下属性: ```properties spring.mail.host=smtp.qq.com spring.mail.username=your_qq_email_address@qq.com spring.mail.password=your_qq_email_auth_code spring.mail.port=587 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 4. 创建JavaMailSender实例。您可以使用Spring Boot提供的JavaMailSender接口创建实例: ```java @Autowired public JavaMailSender emailSender; ``` 5. 编写发送邮件的代码。以下是一个示例: ```java public void sendEmail() { SimpleMailMessage message = new SimpleMailMessage(); message.setTo("recipient@example.com"); message.setSubject("Test Email"); message.setText("Hello, this is a test email."); emailSender.send(message); } ``` 请注意,您可以使用更高级的邮件发送API,例如MimeMessage和MimeMessageHelper,以发送HTML格式的邮件或带有附件的邮件。 希望这可以帮助您在Spring Boot中使用QQ邮件发送

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值