Spring中javaMail通过SMTP发送邮件

1、如果是maven项目,需要引入依赖包


javax.mail
mail
1.4.4

2、在项目加载的basic.xml中,配置两个bean:JavaMailSenderImpl和SimpleMailMessage,在后续发送邮件中通过这两个类进行发送



----


----


smtps


----


----





true
true
smtps






${notify_service.smtp_mail.username}

 

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();
}

===========================================================================

 

转载于:https://www.cnblogs.com/awayInSummer/p/7157106.html

你可能感兴趣的:(Spring中javaMail通过SMTP发送邮件)