| | |
| | | |
| | | import javax.activation.DataHandler; |
| | | import javax.activation.FileDataSource; |
| | | import java.net.URLEncoder; |
| | | import javax.activation.URLDataSource; |
| | | import javax.annotation.Resource; |
| | | import javax.mail.*; |
| | | import javax.mail.internet.*; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.MalformedURLException; |
| | | import java.net.URL; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Properties; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.util.*; |
| | | import java.util.concurrent.CompletableFuture; |
| | | |
| | | @Component |
| | |
| | | // 设置邮件标题 |
| | | message.setSubject("发票"); |
| | | // 创建邮件内容 |
| | | Multipart multipart = createMultipart(list); |
| | | Multipart multipart = new MimeMultipart(); |
| | | // 添加文本消息部分 |
| | | BodyPart messageBodyPart = new MimeBodyPart(); |
| | | messageBodyPart.setHeader("Content-Type", "text/plain;charset=utf-8"); |
| | | messageBodyPart.setContent("您在小程序提交的开票申请已开票成功,请查看附件内容","text/html;charset=UTF-8"); |
| | | multipart.addBodyPart(messageBodyPart); |
| | | List<Path> tempFilePath = new ArrayList<>(); |
| | | // 添加附件部分 |
| | | for (Map<String, String> map : list) { |
| | | messageBodyPart = new MimeBodyPart(); |
| | | String filePath = map.get("filePath"); |
| | | String fileName = map.get("fileName"); |
| | | Path path = Paths.get(filePath, fileName); |
| | | tempFilePath.add(path); |
| | | FileDataSource source = new FileDataSource(path.toString()); |
| | | messageBodyPart.setDataHandler(new DataHandler(source)); |
| | | // String filenameEncode = MimeUtility.encodeText(fileName, "UTF-8", "base64"); |
| | | // String encodedFileName = Base64.getEncoder().encodeToString(fileName.getBytes(StandardCharsets.UTF_8)); |
| | | // String filenameEncode = MimeUtility.encodeText(encodedFileName); |
| | | messageBodyPart.setFileName(fileName); |
| | | messageBodyPart.setHeader("Content-Transfer-Encoding", "base64"); |
| | | messageBodyPart.setHeader("Content-Disposition", "attachment"); |
| | | messageBodyPart.setHeader("Content-Type", "application/octet-stream;name=\"" + fileName + "\""); |
| | | multipart.addBodyPart(messageBodyPart); |
| | | } |
| | | // 设置邮件内容 |
| | | message.setContent(multipart); |
| | | // 发送邮件 |
| | | Transport.send(message); |
| | | // 删除临时目录里面的文件 |
| | | for (Path path : tempFilePath) { |
| | | Files.deleteIfExists(path); |
| | | } |
| | | } catch (MessagingException | UnsupportedEncodingException | MalformedURLException e) { |
| | | log.error("发送邮件发生异常", e); |
| | | throw new ServiceException("发送邮件失败, 请检查"); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("文件下载发生异常"); |
| | | } |
| | | } |
| | | |
| | |
| | | return new PasswordAuthentication(userName, password); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | private Multipart createMultipart(List<Map<String, String>> list) throws MessagingException, UnsupportedEncodingException, MalformedURLException { |
| | | Multipart multipart = new MimeMultipart(); |
| | | // 添加文本消息部分 |
| | | BodyPart messageBodyPart = new MimeBodyPart(); |
| | | messageBodyPart.setHeader("Content-Type", "text/plain;charset=utf-8"); |
| | | messageBodyPart.setContent("您在小程序提交的开票申请已开票成功,请查看附件内容","text/html;charset=UTF-8"); |
| | | multipart.addBodyPart(messageBodyPart); |
| | | // 添加附件部分 |
| | | for (Map<String, String> map : list) { |
| | | messageBodyPart = new MimeBodyPart(); |
| | | String url = map.get("url"); |
| | | String fileName = map.get("fileName"); |
| | | URLDataSource source = new URLDataSource(new URL(url)); |
| | | messageBodyPart.setDataHandler(new DataHandler(source)); |
| | | String filenameEncode = MimeUtility.encodeText(fileName, "UTF-8", "base64"); |
| | | messageBodyPart.setFileName(filenameEncode); |
| | | messageBodyPart.setHeader("Content-Transfer-Encoding", "base64"); |
| | | messageBodyPart.setHeader("Content-Disposition", "attachment"); |
| | | messageBodyPart.setHeader("Content-Type", "application/octet-stream;name=\"" + filenameEncode + "\""); |
| | | multipart.addBodyPart(messageBodyPart); |
| | | } |
| | | |
| | | return multipart; |
| | | } |
| | | |
| | | // public static void main(String[] args) throws UnsupportedEncodingException { |