SpringBoot项目发送邮件&邮箱配置

环境搭建

  1. SpringBoot项目下版本为<version>2.1.5.RELEASE</version>,除基本依赖外需要额外添加发送邮件依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
  1. application.properties文件配置
# MailProperties
spring.mail.host=smtp.163.com(这里使用的是163.com,可自行更换其他邮箱)
spring.mail.port=465
spring.mail.username=需要使用哪个邮箱进行发送
spring.mail.password=邮箱配置里单独生成的密码(不是邮箱密码)
spring.mail.protocol=smtps
spring.mail.properties.mail.smtp.ssl.enable=true

配置类编写

@Component//表示是全局的,通用的
public class MailClient {

    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String to, String subject, String content) {
        try {
//            构建MimeMessage
            MimeMessage message = mailSender.createMimeMessage();
//            帮助类,帮助构建Message
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("发送邮件失败:" + e.getMessage());
        }
    }

}

测试

  1. 测试类下编写如下代码
    @Autowired
    private MailClient mailClient;
//需要发送的目的邮箱地址和内容
    @Test
    public void testTextMail() {
        mailClient.sendMail("20xxxxxxx@qq.com", "TEST", "Welcome.");
    }

邮箱配置

  1. 使用邮箱发送需要自行手动开通邮箱的POP3/SMTP/IMAP配置,这里展示的是网易邮箱,任何邮箱都是需要配置此处,可以自行查找配置。
    在这里插入图片描述

  2. 注意上图,新增的授权码的密码是我们application.properties配置里需要的密码。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在Spring Boot中实现发送邮件,您可以按照以下步骤进行操作: 1. 添加依赖:在您的项目的pom.xml文件中添加Spring Boot的邮件依赖,如下所示: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 配置邮件发送信息:在您的application.properties或application.yml文件中配置SMTP服务器和相应的认证信息,示例如下: ```properties # SMTP服务器地址 spring.mail.host=your-smtp-server # SMTP服务器端口 spring.mail.port=your-smtp-port # 邮件发送者用户名 spring.mail.username=your-username # 邮件发送者密码 spring.mail.password=your-password # 邮件发送者邮箱地址 spring.mail.from=your-email-address ``` 3. 创建邮件发送服务类:创建一个用于发送邮件的服务类,可以使用JavaMailSender类提供的方法来发送邮件。以下是一个简单的示例: ```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 { @Autowired private 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); } } ``` 4. 在需要发送邮件的地方调用服务类:在您的代码中,通过@Autowired注解将邮件发送服务类注入到需要发送邮件的地方,并调用sendEmail方法发送邮件,示例如下: ```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 MyController { @Autowired private EmailService emailService; @GetMapping("/sendEmail") public String sendEmail() { String to = "recipient@example.com"; String subject = "Test Email"; String text = "This is a test email."; emailService.sendEmail(to, subject, text); return "Email sent successfully."; } } ``` 这样,您就可以在Spring Boot应用程序中实现发送邮件功能了。请注意替换相应的SMTP服务器和认证信息,并根据您的需求进行修改和优化代码。希望对您有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值