@Test public void sendAttachmentEmailTest() throws MessagingException { String[] filePathList = new String[2]; filePathList[0] = "/Users/qufei/Documents/study/SpringBoot-WebApi/chapter4.zip"; filePathList[1] = "/Users/qufei/Documents/study/SpringBoot-WebApi/chapter5.zip"; mailService.sendAttachmentMail(to, "发送附件邮件", "hello，这是Spring Boot发送的一封附件邮件!", filePathList); }
public void sendAttachmentMail(String to, String subject, String content, String[] filePathList) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); for (String filePath: filePathList) { System.out.println(filePath); FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath)); String fileName = fileSystemResource.getFilename(); helper.addAttachment(fileName, fileSystemResource); } javaMailSender.send(message); }
MailService { public void sendAttachmentMail(String to, String subject, String content, String[] filePathList) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); for (String filePath: filePathList) { System.out.println(filePath); FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath)); String fileName = fileSystemResource.getFilename(); helper.addAttachment(fileName, fileSystemResource); } javaMailSender.send(message); } }
MailService { public void sendAttachmentMail(String to, String subject, String content, String[] filePathList) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); for (String filePath: filePathList) { System.out.println(filePath); FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath)); String fileName = fileSystemResource.getFilename(); helper.addAttachment(fileName, fileSystemResource); } javaMailSender.send(message); }  }
MailService { public void sendAttachmentMail(String to, String subject, String content, String[] filePathList) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); for (String filePath: filePathList) { System.out.println(filePath); FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath)); String fileName = fileSystemResource.getFilename(); helper.addAttachment(fileName, fileSystemResource); } javaMailSender.send(message); }  void sendTextMail(String to, String subject, String content); void sendHtmlMail(String to, String subject, String content); void sendAttachmentMail(String to, String subject, String content, String[] filePathList); void sendHtmlInlinePhotoMail(String to, String subject, String content, String srcPath, String srcId); }
MailService { public void sendAttachmentMail(String to, String subject, String content, String[] filePathList) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); for (String filePath: filePathList) { System.out.println(filePath); FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath)); String fileName = fileSystemResource.getFilename(); helper.addAttachment(fileName, fileSystemResource); } javaMailSender.send(message); }  void sendTextMail(String to, String subject, String content); void sendHtmlMail(String to, String subject, String content); void sendAttachmentMail(String to, String subject, String content, String[] filePathList); void sendHtmlInlinePhotoMail(String to, String subject, String content, String srcPath, String srcId);  }