<!-- 邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- freemarker begin -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.27-incubating</version>
</dependency>
全局配置文件:
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=2694342466@qq.com
spring.mail.password=uuwzgchukjshddfe
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
package com.email.email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EmailConfig {
@Value("${spring.mail.username}")
private String emailForm;
public String getEmailForm() {
return emailForm;
}
public void setEmailForm(String emailForm) {
this.emailForm = emailForm;
}
}
package com.email.email;
import freemarker.template.TemplateException;
import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;
public interface EmailService {
//发送简单邮件
void sendSimpleMail(String sendTo,String title ,String cont);
//发送带附件的邮件
void sendAttachmentMail(String sendTo, String title , String cont, File file) throws MessagingException;
public void sendTemplateMail(String sendTo, String title,String info) throws MessagingException, IOException, TemplateException;
}
service
package com.email.email;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Service
public class EmailServiceImpl implements EmailService{
@Autowired
private EmailConfig emailConfig;
@Autowired
private JavaMailSender mailSender;
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Override
public void sendSimpleMail(String sendTo, String title,String cont) {
//简单邮件的发送
SimpleMailMessage mailMessage=new SimpleMailMessage();
mailMessage.setFrom(emailConfig.getEmailForm());
mailMessage.setTo(sendTo);
mailMessage.setSubject(title);
mailMessage.setText(cont);
mailSender.send(mailMessage);
}
//发送带附件的邮件
@Override
public void sendAttachmentMail(String sendTo, String title, String cont, File file) throws MessagingException {
MimeMessage msg=mailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(msg,true);
helper.setFrom(emailConfig.getEmailForm());
helper.setTo(sendTo);
helper.setSubject(title);
helper.setText(cont);
FileSystemResource r=new FileSystemResource(file);
helper.addAttachment("附件",r);
mailSender.send(msg);
}
@Override
public void sendTemplateMail(String sendTo, String title,String info) throws MessagingException, IOException, TemplateException {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(msg,true);
helper.setFrom(emailConfig.getEmailForm());
helper.setTo(sendTo);
helper.setSubject(title);
//封装模板数据
Map<String,Object>model=new HashMap<>();
//得到模板
Template template=freeMarkerConfigurer.getConfiguration().getTemplate(info);
String html= FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
helper.setText(html,true);
mailSender.send(msg);
}
}
controller
package com.email.email;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;
@Controller
public class EmailControl {
@Autowired
private EmailService emailService;
@RequestMapping("/simple")
@ResponseBody
public String sendSimpleEmail(){
emailService.sendSimpleMail("[email protected]","你好","明天我去你家玩");
return "Success";
}
@RequestMapping("/attach")
@ResponseBody
public String sendAttachmentMail() throws MessagingException {
File file=new File("src/main/resources/static/mysql (2).txt");
emailService.sendAttachmentMail("[email protected]","你好","明天我去你家玩",file);
return "Success";
}
@RequestMapping("/template")
@ResponseBody
public String sendTemplateMail() throws MessagingException, IOException, TemplateException {
emailService.sendTemplateMail("[email protected]","呵呵","info.html");
return "Success";
}
}
出现认证失败的解决方案:因为JDK1.8中jre\lib\security中两个 jar 包替换的缘故。将下载后的local_policy.jar和US_export_policy.jar替换到JDK1.8的jre\lib\security文件夹即可。
链接: https://pan.baidu.com/s/1hIdNn9FdsFT4i88OHmaeTA
提取码: m55q
整体结构