SpringBoot集成Email发送功能

SpringBoot集成Email发送功能

1.修改pom.xml文件

增加spring-boot-starter-mail

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

2.开启服务获取授权码

登录邮箱,QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码

3.springboot配置文件:

如果使用的是163邮箱,那么要改成 smtp.163.com;
username,发邮件的邮箱地址;
password,授权码;

spring:
  mail:
    host: smtp.qq.com
    username: 123456789@qq.com
    password: xkymkwbmyazubiee
    properties:
      mail:
        smtp:
          auth: true
          timeout: 25000

4.Email配置类:

后期经常使用发件人邮箱,在这统一定义。

package com.zimug.imooc.cms.integration.model;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Email {
    /**
     * 发件邮箱
     */
    @Value("${spring.mail.username}")
    private String emailFrom;

    public String getEmailFrom() {
        return emailFrom;
    }

    public void setEmailFrom(String emailFrom) {
        this.emailFrom = emailFrom;
    }

}

5.Service

定义了发送简单邮件和带附件的邮件

package com.zimug.imooc.cms.integration.service;

import com.zimug.imooc.cms.integration.model.Email;
import org.apache.commons.lang3.tuple.Pair;
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.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;


@Service
public class EmailSendService {

    @Resource
    private Email email;
    @Resource
    private JavaMailSender mailSender;


    /**
     * 发送简单邮件
     */
    public void sendSimpleMail(String sendTo, String titel, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(email.getEmailFrom());
        message.setTo(sendTo);
        message.setSubject(titel);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 发送带附件的邮件
     */
    public void sendAttachmentsMail(String sendTo, String titel, String content, List<Pair<String, File>> attachments)
            throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(email.getEmailFrom());
        helper.setTo(sendTo);
        helper.setSubject(titel);
        helper.setText(content);

        for (Pair<String, File> pair : attachments) {
            helper.addAttachment(pair.getLeft(), new FileSystemResource(pair.getRight()));
        }

        mailSender.send(mimeMessage);
    }

}

6.测试类

springboot 单元测试

package com.zimug.imooc.cms;

import com.zimug.imooc.cms.integration.service.EmailSendService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WebskyApplicationTests {

	@Resource
	EmailSendService emailSendService;

	@Test
	public void sendEmail() {
		String sendTo = "12345678@qq.com";
		String titel = "测试邮件标题";
		String content = "测试邮件内容";

		emailSendService.sendSimpleMail(sendTo,titel,content);
	}


}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值