Spring Boot 2.0.0.M7 发送邮件-使用模板邮件并实现多账号轮询发送

发送邮件-使用模板邮件并实现多账号轮询发送

更多干货

  • spring-boot系列一 之restfull api与多环境配置
  • springboot系列二之 日志
  • SpringBoot系列三之 MVC 模版引擎
  • SpringBoot 2.0.0.M7 系列四 异常处理
  • springboot 2.0.0.M7之 注解 与 配置
  • springboot 2.0.0.M7 配置mvc
  • springboot 2.0.0.M7 之 Servlet Listener Filter
  • springboot 2.0.0.M7 之 跨域
  • springboot 2.0.0.M7 之使用mysql
  • spring boot 2.0.0.M7 之 数据库-事务处理
  • springboot 2.0.0.M7 之 h2 嵌入式数据库的使用
  • springboot 2.0.0.M7 之 数据库-redis
  • Spring Boot 2.0.0.M7 中使用Swagger2构建RESTful API文档
  • Spring Boot 2.0.0.M7 springBoot-mongodb使用
  • Spring Boot 2.0.0.M7 使用 Caching-EhCache
  • Spring Boot spring boot 使用 Caching-Redis
  • Spring Boot 2.0.0.M7 使用异步消息服务-AMQP(RabbitMQ)
  • Spring Boot 2.0.0.M7 调用REST服务-如何使用代理
  • Spring Boot 2.0.0.M7 发送邮件-使用模板邮件并实现多账号轮询发送
  • Spring Boot 2.0.0.M7 使用Spring Session实现集群-redis
  • Spring Boot 2.0.0.M7 如何进行远程调试
  • Spring Boot 生产准备-基于HTTP的监控
  • Spring Boot 集成 Druid
  • springboot思维导图

添加依赖


		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-mailartifactId>
		dependency>

配置

# mail
spring.mail.host: smtp.exmail.qq.com
spring.mail.username:[email protected],[email protected]
spring.mail.password:
spring.mail.properties.mail.smtp.auth: true
# 企业qq的邮箱或者是163这类,不建议使用私人qq

代码实现

实现多账号

/**
 * 实现多账号,轮询发送
 * 
 * @author wujing
 */
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class EduJavaMailSenderImpl extends JavaMailSenderImpl implements JavaMailSender {

	private ArrayList<String> usernameList;
	private ArrayList<String> passwordList;
	private int currentMailId = 0;

	private final MailProperties properties;

	public EduJavaMailSenderImpl(MailProperties properties) {
		this.properties = properties;

		// 初始化账号
		if (usernameList == null)
			usernameList = new ArrayList<String>();
		String[] userNames = this.properties.getUsername().split(",");
		if (userNames != null) {
			for (String user : userNames) {
				usernameList.add(user);
			}
		}

		// 初始化密码
		if (passwordList == null)
			passwordList = new ArrayList<String>();
		String[] passwords = this.properties.getPassword().split(",");
		if (passwords != null) {
			for (String pw : passwords) {
				passwordList.add(pw);
			}
		}
	}

	@Override
	protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {

		super.setUsername(usernameList.get(currentMailId));
		super.setPassword(passwordList.get(currentMailId));

		// 设置编码和各种参数
		super.setHost(this.properties.getHost());
		super.setDefaultEncoding(this.properties.getDefaultEncoding().name());
		super.setJavaMailProperties(asProperties(this.properties.getProperties()));
		super.doSend(mimeMessage, object);

		// 轮询
		currentMailId = (currentMailId + 1) % usernameList.size();
	}

	private Properties asProperties(Map<String, String> source) {
		Properties properties = new Properties();
		properties.putAll(source);
		return properties;
	}

	@Override
	public String getUsername() {
		return usernameList.get(currentMailId);
	}

}

实现发送功能

@Component
publicclass EduJavaMailComponent {
	privatestaticfinal String template = "mail/ctoEdu.ftl";

	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	@Autowired
	private EduJavaMailSenderImpl javaMailSender;

	publicvoid sendMail(String email) {
		Map<String, Object>map = new HashMap<String, Object>();
		map.put("email", email);
		try {
			String text = getTextByTemplate(template, map);
			send(email, text);
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	private String getTextByTemplate(String template, Map<String, Object>model) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		return FreeMarkerTemplateUtils.processTemplateIntoString(freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
	}

	private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
		MimeMessage message = javaMailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		InternetAddress from = new InternetAddress();
		from.setAddress(javaMailSender.getUsername());
		from.setPersonal("ctoedu", "UTF-8");
		helper.setFrom(from);
		helper.setTo(email);
		helper.setSubject("测试邮件");
		helper.setText(text, true);
		javaMailSender.send(message);
		returntext;
	}

}

你可能感兴趣的:(【springboot】)