1、如果是maven项目,需要引入依赖包
2、在项目加载的basic.xml中,配置两个bean:JavaMailSenderImpl和SimpleMailMessage,在后续发送邮件中通过这两个类进行发送
private MailSender mailSender;
private SimpleMailMessage simpleMailMessage;
//初始化mailSender 和SimpleMailMessage 两个对象
simpleMailMessage.setTo(sendTo);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
mailSender.send(simpleMailMessage);
另外如果是发送HTML格式的邮件
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
private JavaMailSender javaMailSender;
private MimeMessage mimeMessage;
private MimeMessageHelper mimeMessageHelper;
private SimpleMailMessage simpleMailMessage;
try {
mimeMessage = javaMailSender.createMimeMessage();
mimeMessageHelper = new MimeMessageHelper(mimeMessage,true,"GBK");
mimeMessageHelper.setFrom(simpleMailMessage.getFrom());
mimeMessageHelper.setTo(sendTo);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
===========================================================================