Springboot 发送邮件

目录

一、springboot整合javaMail

(1)导入依赖:

(2)做配置

(3)邮件客户端

二、发送复杂文件


一、springboot整合javaMail

(1)导入依赖:

        
            org.springframework.boot
            spring-boot-starter-mail
        

(2)做配置

spring:
  mail:
    host: smtp.qq.com #邮件服务器供应商
    username: 129*****[email protected]
    password: jcf****fb

password到对应的供应商邮箱开启smtp配置和pop3配置

qq邮箱为:设置——>

Springboot 发送邮件_第1张图片

点击开启会查询邮件确认生份,确认生份后便可获得该密码。

(3)邮件客户端

package com.example.demo.service.impl;

import com.example.demo.service.SendMail;
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 SendMailImpl implements SendMail {
    @Autowired
    private JavaMailSender javaMailSender;

    //发送人
    private String from="129******[email protected]";
    //接收人
    private String to="wu*****@163.com";
    //标题
    private String subject="测试邮件";
    //正文
    private String text="邮件正文内容";

    @Override
    public void sendMail() {
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(from+"(大表哥)");//后面的为名称
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
//        message.setSentDate();//定时发送
        javaMailSender.send(message);
    }
}

JavaMailSender对象是spring容器里添加依赖后生成的,可直接注入使用。

SimpleMailMessage对象里可添加信息,基本信息如上。 

使用该方法便可以发送到目的邮箱了!

二、发送复杂文件

配置和简单文件发送一样,业务层如下

package com.example.demo.service.impl;

import com.example.demo.service.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

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

@Service
public class SendMailImpl2 implements SendMail {
    @Autowired
    private JavaMailSender javaMailSender;

    //发送人
    private String from = "12**[email protected]";
    //接收人
    private String to = "wu******@163.com";
    //标题
    private String subject = "测试邮件";
    //正文
    private String text = "点开有惊喜";

    @Override
    public void sendMail() {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            //设置为true代表允许上传附件
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from + "(大表哥)");
            helper.setTo(to);
            helper.setSubject(subject);
            //设置为true代表允许发送html文本
            helper.setText(text, true);

            //发送附件
            File f1 = new File("C:\\Users\\Gragon wu\\Desktop\\网站.txt");
            File f2 = new File("C:\\Users\\Gragon wu\\Desktop\\捕获.PNG");
            helper.addAttachment(f1.getName(), f1);
            helper.addAttachment("第二个文件.png", f2);

            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这里使用了MimeMessage对象和MimeMessageHelper对象来实现。

你可能感兴趣的:(springboot,spring,boot,java,后端)