package com.www.commons.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
/**
* @author Sunshine
* @description实现邮件发送
* 2017-12-20上午9:27:39
*/
@Service("mailBiz")
publicclass MailBiz {
@Autowired
private JavaMailSender mailSender;
@Autowired
private SimpleMailMessage simpleMailMessage;
//注入线程池对象
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
/**
* 发送邮件
*/
public void sendMail(final MailModel mm) {
taskExecutor.execute(new Runnable() {
@Override
publicvoid run() {
try {
/*simpleMailMessage.setFrom(mm.getFrom());//设置发件人
*/ simpleMailMessage.setTo(mm.getTo());//设置收件人
simpleMailMessage.setSubject(mm.getSubject());//主题
simpleMailMessage.setText(mm.getContent());//内容
mailSender.send(simpleMailMessage);
} catch (Exception e) {
System.out.println("邮件发送异常");
e.printStackTrace();
}
System.out.println("邮件发送成功!");
}
});
}
}