SpringBoot整合邮件发送服务

简介: SpringBoot整合邮件发送服务

开通邮件发送服务


配置文件

# 邮箱发送服务配置
spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=你的邮箱号码
spring.mail.password=授权码
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000

配置类

package com.dam.config.mail;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
 * 读取yml配置文件里面的信息
 */
@Configuration
@ConfigurationProperties(prefix = "spring.mail")
@Data
public class MailConfig {
//    private String email;
    private String host;
    private String port;
    private String username;
    private String password;
}


Dto类

package com.dam.model.dto.third_party;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public
class EmailDto {
    /**
     * 接收邮箱
     */
    private String to;
    /**
     * 主题
     */
    private String subject;
    /**
     * 内容
     */
    private String content;
}


Service

package com.dam.service;
import com.dam.model.dto.third_party.EmailDto;
public interface EmailService {
    /**
     * 发送邮件
     *
     * @param emailDto 邮箱列表
     */
    boolean send(EmailDto emailDto);
}


package com.dam.service.impl;
import com.dam.config.mail.MailConfig;
import com.dam.model.dto.third_party.EmailDto;
import com.dam.service.EmailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class EmailServiceImpl implements EmailService {
    @Autowired
    private MailConfig mailConfig;
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Override
    public boolean send(EmailDto emailDto) {
        String fromEmail = mailConfig.getUsername();
        //定制纯文本邮件信息SimpleMailMessage
        SimpleMailMessage message = new SimpleMailMessage();
        try {
            //设置发件箱
            message.setFrom(fromEmail);
            //设置收件箱
            message.setTo(emailDto.getTo());
            //设置邮件主题
            message.setSubject(emailDto.getSubject());
            //设置邮件内容
            message.setText(emailDto.getContent());
            //调用Java封装好的发送方法
            mailSender.send(message);
            return true;
        } catch (Exception e) {
            System.out.println("邮件发送失败,原因:" + e.getMessage());
            return false;
        }
    }
}

Controller

package com.dam.controller;
import com.dam.model.dto.third_party.EmailDto;
import com.dam.model.enums.ResultCodeEnum;
import com.dam.model.result.R;
import com.dam.service.EmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
@RequestMapping("/thirdParty/mail")
@Api(tags = "邮件服务")
public class MailController {
    @Autowired
    private EmailService emailService;
    @ApiOperation(value = "发送邮件")
    @PostMapping("/send")
    public R send(@RequestBody EmailDto emailDto) {
        boolean isSuccess = emailService.send(emailDto);
        if (isSuccess) {
            return R.ok();
        } else {
            return R.error(ResultCodeEnum.FAIL);
        }
    }
}


目录
相关文章
|
5天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
62 0
|
5天前
|
Web App开发 监控 Java
|
5天前
|
NoSQL Java Redis
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
379 1
|
5天前
|
Dubbo Java 应用服务中间件
Spring Boot Dubbo 构建分布式服务
Spring Boot Dubbo 构建分布式服务
54 0
|
5天前
|
监控 安全 Java
SpringBoot-开启Admin监控服务
本文介绍如何在SpringBoot项目中开启Admin监控服务。
57 0
|
5天前
|
前端开发 Java 数据库连接
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
34 0
|
5天前
|
Java API 数据安全/隐私保护
【亮剑】如何在Java项目中结合Spring框架实现邮件发送功能
【4月更文挑战第30天】本文介绍了如何在Java项目中结合Spring框架实现邮件发送功能。首先,需在`pom.xml`添加Spring和JavaMail依赖。然后,在`applicationContext.xml`配置邮件发送器,包括SMTP服务器信息。接着,创建一个使用依赖注入的`EmailService`类,通过`JavaMailSender`发送邮件。最后,调用`EmailService`的`sendSimpleEmail`方法即可发送邮件。最佳实践包括:使用配置管理敏感信息,利用`MimeMessage`构造复杂邮件,异常处理和日志记录,以及在大量发送时考虑使用邮件队列。
|
5天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的医院核酸检测服务系统的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的医院核酸检测服务系统的详细设计和实现
39 0
|
5天前
|
JSON 前端开发 安全
Springboot整合邮件服务
Springboot整合邮件服务
|
5天前
|
Java Spring
使用spring实现邮件的发送(含测试,源码,注释)
使用spring实现邮件的发送(含测试,源码,注释)
7 0