以下演示的是简单邮件发送
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <!-- Email依赖 --> 7 <dependency> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-mail</artifactId> 10 </dependency> 11 12 <!-- freemarker的依赖 --> 13 <dependency> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-starter-freemarker</artifactId> 16 </dependency>
1 spring.mail.host=smtp.qq.com 2 spring.mail.username=your@qq.com 3 #授权码 4 spring.mail.password= 5 spring.mail.properties.mail.smtp.auth=true 6 spring.mail.properties.mail.smtp.starttls.enable=true 7 spring.mail.properties.mail.smtp.starttls.required=true
1 package com.wu.service; 2 3 import java.io.File; 4 5 public interface EmailService { 6 //发送简单的邮件 7 public void sendSimpleEmailService(String sendTo,String title,String content); 8 9 //发送带有附件的邮件 10 public void sendAttachmentMail(String sendTo,String title,String content,File file); 11 12 //发送模板邮件 13 public void sendTemplateEmailService(String sendTo,String title,String content); 14 }
1 package com.wu.service; 2 3 import java.io.File; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import javax.mail.MessagingException; 8 import javax.mail.internet.MimeMessage; 9 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Value; 12 import org.springframework.core.io.FileSystemResource; 13 import org.springframework.mail.SimpleMailMessage; 14 import org.springframework.mail.javamail.JavaMailSender; 15 import org.springframework.mail.javamail.MimeMessageHelper; 16 import org.springframework.stereotype.Service; 17 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; 18 import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; 19 import freemarker.template.Template; 20 21 @Service 22 public class EmailServiceImp implements EmailService { 23 @Autowired 24 private JavaMailSender sender; 25 26 @Autowired 27 private FreeMarkerConfigurer fc; 28 29 @Value("${spring.mail.username}") 30 private String sendFrom; 31 32 @Override 33 public void sendSimpleEmailService(String sendTo, String title, String content) { 34 SimpleMailMessage message=new SimpleMailMessage(); 35 message.setFrom(sendFrom); 36 message.setTo(sendTo); 37 message.setSubject(title); 38 message.setText(content); 39 sender.send(message); 40 } 41 42 @Override 43 public void sendAttachmentMail(String sendTo, String title, String content, File file) { 44 MimeMessage message=sender.createMimeMessage(); 45 try { 46 MimeMessageHelper helper=new MimeMessageHelper(message,true);//true为设置为multipart模式 47 helper.setFrom(sendFrom); 48 helper.setTo(sendTo); 49 helper.setSubject(title); 50 helper.setText(content); 51 FileSystemResource fsResource=new FileSystemResource(file); 52 helper.addAttachment("这是一个附件",fsResource); 53 sender.send(message); 54 } catch (MessagingException e) { 55 e.printStackTrace(); 56 } 57 } 58 59 @Override 60 public void sendTemplateEmailService(String sendTo, String title, String content){ 61 MimeMessage message=sender.createMimeMessage(); 62 try { 63 MimeMessageHelper helper=new MimeMessageHelper(message,true); 64 helper.setTo(sendTo); 65 helper.setFrom(sendFrom); 66 helper.setSubject(title); 67 68 //封装模板数据 69 Map<String,Object> map=new HashMap<>(); 70 map.put("username","工具人"); 71 72 73 //得到模板并将数据加入到模板中 74 Template template = fc.getConfiguration().getTemplate(content); 75 String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, map); 76 77 //封装邮件模板 78 helper.setText(html); 79 80 //发送邮件 81 sender.send(message); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 87 }
1 package com.wu.controller; 2 3 import java.io.File; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 import com.wu.service.EmailService; 9 10 @RestController 11 public class EmailController { 12 @Autowired 13 private EmailService service; 14 15 //发送简单邮件 16 @RequestMapping("/sendSimpleEmail") 17 public String sendSimpleEmail(){ 18 service.sendSimpleEmailService("toSend@gmail.com","这是一个测试","请忽略!"); 19 return "success"; 20 } 21 22 //发送带有附件的邮件 23 @RequestMapping("/sendAttachmentEmail") 24 public String sendAttachmentEmail(){ 25 File file=new File("src/main/resources/static/test.txt"); 26 service.sendAttachmentMail("toSend@gmail.com","这也是一个测试","请忽略",file); 27 return "success again"; 28 } 29 30 //发送模板邮件 31 @RequestMapping("/sendTemplateEmailService") 32 public String sendTemplateEmailService(){ 33 service.sendTemplateEmailService("toSend@gmail.com","这是模板邮件","info.html"); 34 return "success again again"; 35 } 36 }
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <!-- Thymeleaf模板 --> 7 <dependency> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-thymeleaf</artifactId> 10 </dependency>
1 #单个文件大小 2 multipart.maxFileSize=50Mb 3 #接收的所有文件大小 4 multipart.maxRequestSize=500Mb
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"/> 5 <title>上传文件</title> 6 </head> 7 <body> 8 <p>单个文件上传</p> 9 <form action="/upload" enctype="multipart/form-data" method="post"> 10 <input type="file" name="file"/><br></br> 11 <input type="submit" value="提交"/> 12 </form> 13 <hr></hr> 14 <p>批量文件上传</p> 15 <form action="/uploads" enctype="multipart/form-data" method="post"> 16 <input type="file" name="file"/><br></br> 17 <input type="file" name="file"/><br></br> 18 <input type="file" name="file"/><br></br> 19 <input type="submit" value="提交"/> 20 </form> 21 </body> 22 </html>
1 package com.wu.controller; 2 3 import java.io.File; 4 import java.util.UUID; 5 import javax.servlet.http.HttpServletRequest; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.multipart.MultipartFile; 10 11 @Controller 12 public class UploadController { 13 @RequestMapping("/index") 14 public String upload(){ 15 return "upload"; 16 } 17 18 @ResponseBody 19 @RequestMapping("/upload") 20 public String uploadFile(MultipartFile file,HttpServletRequest request){ 21 //路径 22 String dir = request.getServletContext().getRealPath("/upload"); 23 File fileDir=new File(dir); 24 if(!fileDir.exists()){ 25 fileDir.mkdir(); 26 } 27 //文件名 28 String suffex = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); 29 String fileName=UUID.randomUUID().toString()+suffex; 30 File file2=new File(fileDir+fileName); 31 try { 32 file.transferTo(file2); 33 } catch (Exception e) { 34 e.printStackTrace(); 35 return "上传失败"; 36 } 37 return "上传成功"; 38 } 39 40 @ResponseBody 41 @RequestMapping("/uploads") 42 public String uploadFiles(MultipartFile file[],HttpServletRequest request){ 43 //路径 44 String dir = request.getServletContext().getRealPath("/upload"); 45 File fileDir=new File(dir); 46 if(!fileDir.exists()){ 47 fileDir.mkdir(); 48 } 49 //文件名 50 for(int i=0;i<file.length;i++){ 51 String suffex = file[i].getOriginalFilename().substring(file[i].getOriginalFilename().lastIndexOf(".")); 52 String fileName=UUID.randomUUID().toString()+suffex; 53 File file2=new File(fileDir+fileName); 54 try { 55 file[i].transferTo(file2); 56 } catch (Exception e) { 57 e.printStackTrace(); 58 return "上传失败"; 59 } 60 } 61 62 return "上传成功"; 63 } 64 }
原文:https://www.cnblogs.com/wuba/p/11242656.html