其他分享
首页 > 其他分享> > Spring Boot 邮件发送

Spring Boot 邮件发送

作者:互联网

Spring Boot 邮件发送

以163为例,需要开启163邮箱授权码.

导入邮件模块依赖

 <!--邮件模块依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

配置文件

application.yml

spring:
  mail:
    host: smtp.163.com #发送邮件服务器
    username: xxx@163.com #发送邮件的邮箱
    password: xxx #163授权码
    properties.mail.smtp.port: 994
    from: xxxx@163.com #发送邮件的邮箱 同上
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8

EmailServiceImpl.java

实现EmailService接口

import com.xmx.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

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

@Service
public class EmailServiceImpl implements EmailService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JavaMailSender javaMailSender;

    //注入配置文件中配置的信息——>from
    @Value("${spring.mail.from}")
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        message.setFrom(from);
        //收件人
        message.setTo(to);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        javaMailSender.send(message);
    }
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message,true);
            messageHelper.setFrom(from);
            messageHelper.setTo(to);
            message.setSubject(subject);
            messageHelper.setText(content,true);
            javaMailSender.send(message);
            logger.info("邮件已经发送!");
        } catch (MessagingException e) {
            logger.error("发送邮件时发生异常:"+e);
        }
    }
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message,true);
            messageHelper.setFrom(from);
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setText(content,true);
            //携带附件
            FileSystemResource file = new FileSystemResource(filePath);
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            messageHelper.addAttachment(fileName,file);

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

EmailService.java

接口类

public interface EmailService {

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

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

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

EmailServiceTest.java

测试类

public class EmailServiceTest{

    @Autowired
    private EmailService emailService;

    @Test
    public void sendSimpleEmail(){
        //文章内容
        String content = "你好,恭喜你...";
        //发送人邮箱,标题,文章内容
        emailService.sendSimpleMail("171454xxxx@qq.com","祝福邮件",content);
    }

    @Test
    public void sendMimeEmail(){
        String content = "<a href='https://blog.csdn.net/weixin_43976075/article/details/118891984'>范女士,你好!这里有一封来自你农场的信!快给他回复吧!</a>";
        emailService.sendHtmlMail("2418375xxx@qq.com","摩尔庄园",content);
    }

    @Test
    public void sendAttachment(){
        emailService.sendAttachmentsMail("XX@qq.com","发送附件","这是Java体系图","F:/图片/爱旅行.jpg");
    }
}

标签:String,Spring,Boot,content,messageHelper,import,message,邮件,subject
来源: https://blog.csdn.net/weixin_43976075/article/details/118926191