SpringBoot项目集成邮件功能

  |   0 评论   |   445 浏览

好久没更新了,出来冒个泡。2019第一贴,祝各位猪年顺利。

1.pom文件依赖配置

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

2.application.properties配置

#服务器
risk.mail.host=smtp.xxx.com
#端口
risk.mail.port=25
#账户
risk.mail.userNumber=xxxxxxx@xxx.com
#密码
risk.mail.userPassWord=xxxxxxx
#时间延迟
risk.mail.timeOut=25000
#接收邮件的账户(用逗号分隔可设置多账户,例:xxx@xx.com,xxx@xx.com)
risk.mail.acceptNumber=bbbbbb@yhfin.com,aaaaaa@qq.com

3.MailUtil.java

package com.yhfin.risk.engine.utils;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class MailUtil {
	@Value("${risk.mail.host}")
	private String host;
	@Value("${risk.mail.port}")
	private String port;
	@Value("${risk.mail.userNumber}")
	private String userNumber;//发送账户
	@Value("${risk.mail.userPassWord}")
	private String userPassWord;
	@Value("${risk.mail.timeOut}")
	private String timeOut;
	@Value("${risk.mail.acceptNumber}")
	private String acceptNumber;//接收账户
	private static String emailForm;
	private static String personal;
	private static MailProperties mailProperties;
	@PostConstruct
	private void init() {
		MailUtil.emailForm = this.userNumber;
		MailUtil.personal = "主题";
		mailProperties = new MailProperties(this.host,this.port,this.userNumber,this.userPassWord,this.timeOut,this.acceptNumber);
	}
	private static JavaMailSenderImpl createMailSender() {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		sender.setHost(mailProperties.host);
		sender.setPort(Integer.valueOf(mailProperties.port));
		sender.setUsername(mailProperties.userNumber);
		sender.setPassword(mailProperties.userPassWord);
		Properties p = new Properties();
		p.setProperty("mail.smtp.timeout", mailProperties.timeOut);
		p.setProperty("mail.smtp.auth", "false");
		sender.setJavaMailProperties(p);
		return sender;
	}
	private static class MailProperties {
		private String host;
		private String port;
		private String userNumber;
		private String userPassWord;
		private String timeOut;
		private String acceptNumber;
		private MailProperties(String host, String port, String userNumber, String userPassWord,
				String timeOut,String acceptNumber) {
			this.host = host;
			this.port = port;
			this.userNumber = userNumber;
			this.userPassWord = userPassWord;
			this.timeOut = timeOut;
			this.acceptNumber = acceptNumber;
		}
	}
	/**
	 * 发送邮件
	 * @param subject
	 * @param html
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 */
	public static void sendMail(String subject, String html) throws MessagingException,UnsupportedEncodingException {
		JavaMailSenderImpl mailSender = createMailSender();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(emailForm, personal);
        messageHelper.setTo(mailProperties.acceptNumber.split(","));//setTo方法的参数传入存放邮箱账号的数组便是群发
        messageHelper.setSubject(subject);
        messageHelper.setText(html, true);
        mailSender.send(mimeMessage);
    }
}

4.简单测一下

package com.yhfin.risk.server.controller;
import java.io.UnsupportedEncodingException;
import javax.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.yhfin.risk.engine.utils.MailUtil;
@RestController
@RequestMapping("/test/TestController")
public class TestController {
	private static Logger logger =  LoggerFactory.getLogger(TestController.class);
	@RequestMapping(value = "/testMail", method = RequestMethod.POST, produces = "application/json")
	public void testMail() {
		try {
			MailUtil.sendMail("test", "邮件测试");
		} catch (UnsupportedEncodingException | MessagingException e) {
			e.printStackTrace();
			logger.error("通知邮件发送失败");
		}
	}
}

这个家伙很懒,连签名都不写。

评论

发表评论