Spring中默认有starter实现类Mail
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
配置文件
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=用户qq邮箱
#QQ邮箱的授权码
spring.mail.password=密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8
简单邮件
public void sendSimpleMail(){
try{
SimpleMaileMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage .setFrom("sendFrom@qq.com");
simpleMailMessage .setTo("sendTo@qq.com");
simpleMailMessage.setSubject("主题");
simpleMailMessage.setText("内容");
javaMailSender.send(simpleMailMessage);//发送
}catch(Exception e){
logger.error("发送失败",e.getMessage());
}
}
自定义发件人名称
String nick = "";
try {
nick = javax.mail.internet.MimeUtility.encodeText("张三");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 设置发件人
mimeMsg.setFrom(new InternetAddress("sendFrom@qq.com", nick));
html格式邮件
public void sendHTMLMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
mimeMessageHelper.setText("<h1>hello</h1>", true);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("邮件发送失败", e.getMessage());
}
}
带附件格式的邮件
public void sendAttachmentMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
mimeMessageHelper.setText(mailBean.getContent());
//文件路径 spring的FileSystemResource,使用绝对路径访问文件资源
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.png"));
mimeMessageHelper.addAttachment("mail.png", file);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("邮件发送失败", e.getMessage());
}
}
发送带静态资源的邮件
public void sendInlineMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
mimeMessageHelper.setText("<html><body>带静态资源的邮件内容,这个一张IDEA配置的照片:<img src='cid:picture' /></body></html>", true);
//文件路径
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.png"));
mimeMessageHelper.addInline("picture", file);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("邮件发送失败", e.getMessage());
}
}
基于Freemarker的模板邮件
public void sendTemplateMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
Map<String, Object> model = new HashMap<String, Object>();
model.put("content", mailBean.getContent());
model.put("title", "标题Mail中使用了FreeMarker");
Template template = configuration.getTemplate("mail.ftl");
String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
mimeMessageHelper.setText(text, true);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("邮件发送失败", e.getMessage());
}
}
动态添加邮箱配置信息
和之前使用接口javaMailSender
不同,这次使用的是接口的实现类。
使用JavaMailSenderImpl
public void sendSimpleMail() throws Exception {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();//直接生产一个实例
String users[] = {"xxxxx@qq.com","xxxxx@126.com"};
mailSender.setHost("smtp.126.com");//动态添加配置
mailSender.setPassword("xxxxx");
mailSender.setPort(25);
mailSender.setProtocol("smtp");
mailSender.setUsername("xxxxx");
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("xxxxxxx");
message.setTo(users); // 使用数组的形式还可以群发
message.setSubject("羽毛球比赛");
message.setText("报名参加羽毛球赛");
mailSender.send(message);
}