SpringBoot发送邮件

本文详细介绍了如何使用SpringBoot与QQ邮箱进行集成,包括开启邮箱授权码、配置SMTP服务,以及实现发送文本邮件、HTML邮件和带附件的功能。通过实例代码演示了如何调用相关API并处理异常情况。
摘要由CSDN通过智能技术生成

第一步:QQ邮箱开启邮箱授权码

在这里插入图片描述
切换到账户选项卡:
在这里插入图片描述
拉到下方 找到POP3/IMAP/SMTP服务,开启POP3/SMTP服务:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
oxdlgmtkqtnbbjdg

第二步:导入依赖

创建SpringBoot项目,导入Maven依赖:

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

第三步:配置文件

spring:
  mail:
    host: smtp.qq.com #发送邮件服务器
    username: 369950806@qq.com  #发送邮件的邮箱地址
    password: oxdlgmtkqtXXXXX   #客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的
    properties.mail.smtp.port: 465 #端口号465587
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8

:上面配置信息需要换成自己的。

第四步:接口

public interface EmailService {

    /**
     * 发送文本邮件
     *
     * @param from    发送人
     * @param to      收件人
     * @param cc      抄送人
     * @param subject 主题
     * @param content 内容
     */
    void sendTextEmail(String from,String to,String cc, String subject, String content);

    /**
     * 发送HTML邮件
     *
     * @param from    发送人
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    void sendMimeEmail(String from,String to, String subject, String content);

    /**
     * 发送带附件的邮件
     *
     * @param from    发送人
     * @param to       收件人
     * @param subject  主题
     * @param content  内容
     * @param file 附件
     */
    public void sendsAttachmentEmail(String from,String to, String subject, String content, String file);
}

第五步:实现类

@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender sender;

    @Override
    public void sendTextEmail(String from,String to, String cc, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        message.setFrom(from);
        //收件人
        message.setTo(to);
        //抄送人
        message.setCc(cc);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        sender.send(message);
    }

    @Override
    public void sendMimeEmail(String from,String to, String subject, String content) {
        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            message.setSubject(subject);
            helper.setText(content,true);
            sender.send(message);
            log.info("邮件已经发送!");
        } catch (MessagingException e) {
            log.error("发送邮件时发生异常:"+e);
        }
    }

    @Override
    public void sendsAttachmentEmail(String from,String to, String subject, String content, String file) {
        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            //携带附件
            FileSystemResource fsr = new FileSystemResource(file);
            String fileName = file.substring(file.lastIndexOf("/"));
            helper.addAttachment(fileName,fsr);

            sender.send(message);
            log.info("邮件加附件发送成功!");
        } catch (MessagingException e) {
            log.error("发送失败:"+e);
        }
    }

}

第六步:测试代码

@SpringBootTest
class MailDemoApplicationTests {

    @Resource
    private EmailService emailService;

    @Test
    public void sendSimpleEmail(){
        String content = "好好学习,天天向上";
        emailService.sendTextEmail("369950806@qq.com","3433698914@qq.com","hcitlife@hotmail.com","哈哈",content);
    }

    @Test
    public void sendMimeEmail(){
        String content = "<a href='https://blog.csdn.net/lianghecai52171314/'>注册成功,点击链接激活</a>";
        emailService.sendMimeEmail("369950806@qq.com","3433698914@qq.com","激活邮件",content);
    }

    @Test
    public void sendsAttachmentEmail1(){
        emailService.sendsAttachmentEmail("369950806@qq.com","3433698914@qq.com","带附件的邮件","美女","E:/图片/mm01.jpg");
    }

    @Test
    public void sendsAttachmentEmail2(){
        emailService.sendsAttachmentEmail("369950806@qq.com","3433698914@qq.com","带附件的邮件","美女","E:/图片/zbjbxf.mp3");
    }

    @Test
    void fun(){
        File file = new File("E:/图片/mm01.jpg");
        System.out.println(file.getName());
    }

}
Spring Boot发送邮件通常通过JavaMail API实现,Spring Boot提供了一个方便的集成方式,无需额外配置SMTP服务器。以下是使用Spring Boot发送邮件的基本步骤: 1. 添加依赖:在`pom.xml`文件中添加Spring Boot Actuator和JavaMail的相关依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 配置邮箱服务:在application.properties或application.yml文件中设置SMTP服务器的信息,如主机名、端口、用户名、密码等: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=your-email@example.com spring.mail.password=your-password spring.mail.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建邮件消息:创建一个Java类,继承`AbstractMessageConverter`或使用`SimpleMailMessage`来构建邮件内容: ```java import org.springframework.mail.SimpleMailMessage; SimpleMailMessage message = new SimpleMailMessage(); message.setTo("recipient@example.com"); message.setFrom("sender@example.com"); message.setSubject("Hello from Spring Boot"); message.setText("This is a test email."); ``` 4. 使用Java配置或注解:在Spring Boot应用中,你可以使用Java配置类配置一个`JavaMailSender`实例,然后在需要的地方发送邮件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; @Autowired private JavaMailSender javaMailSender; public void sendEmail(SimpleMailMessage message) { javaMailSender.send(message); } ``` 或者使用`@Autowired`自动注入并在方法上使用`@SendMail`注解: ```java @RestController public class EmailController { @Autowired private JavaMailSender javaMailSender; @PostMapping("/send-email") @SendMail public ResponseEntity<String> sendMessage(SimpleMailMessage message) { // ...处理并返回响应 } } ```
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁云亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值