spring boot 发送邮箱

此篇介绍 spring boot 集成 email 发送邮箱 这里 用的是qq邮箱
添加 maven 依赖

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

添加配置文件:

spring:

  mail: 
    host: smtp.qq.com
    #你的QQ邮箱账户
    username: [email protected]
    #你的QQ邮箱第三方授权码
    password: xxx
    #编码类型
    default-encoding: UTF-8

QQ邮箱第三方授权码 需要在 QQ邮箱获取
需要在 设置中找到账户的最下面

在这里插入图片描述
一般开启第一个就可以了
在这里插入图片描述

我这 创建了 server 和 controller 目的为了配合 swagger 使用

@Service
public class EmailServer {

	@Autowired
    private JavaMailSender mailSender;
	
	@Value("${spring.mail.username}")
    private String from;
	
	//普通邮件
	public Map<String, Object> sendToEmail(String to, String title, String content) {
		Map<String, Object> mp = new HashMap<String, Object>();
		SimpleMailMessage message = new SimpleMailMessage();
		if(to != null && title != null && content != null) {
			try {
				message.setTo(to);//收信人
		        message.setSubject(title);//主题
		        message.setText(content);//内容
		        message.setFrom(from);//发信人
		        mailSender.send(message);
		        mp.put("msg", 200);
			} catch (Exception e) {
				mp.put("msg", 500);
			}
		}else {
	        mp.put("msg", 1017);
		}
        return mp;
	}
	//HTML邮件
	public Map<String, Object> sendToHTMLEmail(String to, String title, String content) {
		Map<String, Object> mp = new HashMap<String, Object>();
		if(to != null && title != null && content != null) {
			try {
				MimeMessage message = mailSender.createMimeMessage();
		        MimeMessageHelper helper = new MimeMessageHelper(message, true);
	            helper.setTo(to);
	            helper.setSubject(title);
	            helper.setText(content, true);//true代表支持html
	            helper.setFrom(from);
	            mailSender.send(message);
		        mp.put("msg", 200);
			} catch (Exception e) {
				mp.put("msg", 500);
			}
		}else {
	        mp.put("msg", 1017);
		}
        return mp;
	}
	
}
@RestController
@RequestMapping("/email")
@Api(tags = "邮件相关接口")
@CrossOrigin(origins = "*", maxAge = 3600)
public class EmailController{

	@Autowired
	protected EmailServer email;//邮箱
	
	@ApiOperation(value="发送简单邮箱")
    @PostMapping("/sendToEmail")
    public Map<String, Object> sendToEmail(@RequestBody Map<String, Object> map) {
        return email.sendToEmail((String)map.get("to"), (String)map.get("title"), (String)map.get("content"));
    }

	@ApiOperation(value="发送HTML邮箱")
    @PostMapping("/sendToHTMLEmail")
    public Map<String, Object> sendToHTMLEmail(@RequestBody Map<String, Object> map) {
        return email.sendToHTMLEmail((String)map.get("to"), (String)map.get("title"), (String)map.get("content"));
    }
	
}

在这里插入图片描述
在这里插入图片描述
推荐一个比较全的spring boot 发送邮箱

//发送图片需要添加
                File file = new File("img_url");
             FileSystemResource res = new FileSystemResource(file);
             helper.addInline("001", res);
 
                //测试时加入 <img width='250px' src='cid:001'>

猜你喜欢

转载自blog.csdn.net/weixin_42118284/article/details/90665412
今日推荐