package com.ssj.taskasync;

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.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SyncSendMailTask {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private JavaMailSender mailSender;
	
	@Value("${spring.mail.username}")
	private String from;

	/**
	 * from //发送者列表
	 * @param // 邮件接受者
	 * @param subject // 主题
	 * @param content// 内容
	 */
	@Async("taskExecutor")
	public void run(List<String> tos,String subject,String content) {
		logger.error("立即Mail发送任务开始---------------");
		try {
			SimpleMailMessage message =null;
			for (String to : tos) {
				message = new SimpleMailMessage();
		        message.setFrom(from);
		        message.setTo(to); 
		        message.setSubject(subject);
		        message.setText(content); 
		        mailSender.send(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("立即Mail发送任务异常...");
		}
		logger.error("立即Mail发送任务结束---------------");
	}

	
	
	

}
