在 Spring Boot 中将多部分文件作为电子邮件的附件添加

Adding multipart file as an attachment to an email in Spring Boot

我想从 Spring Boot 发送一封带有 pdf 附件的电子邮件。我从 POST 电话中收到了作为多部分文件的 pdf 文件。

到目前为止,这是我的控制器 class(sendEmails 方法包含在 emailService 服务中):

@PostMapping("/email")
    public ResponseEntity<?> sendEmail(@RequestParam("file") MultipartFile pdfFile,
                                       @RequestParam("email") String email) {


        boolean result = this.emailService.sendEmails(email, pdfFile);
        
        if (result) {
            return ResponseEntity.ok("Email sent...");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Email sending failed");
        }
    }

这里是 sendEmails 方法:

public boolean sendEmails(String reciever, MultipartFile pdf) {

        try {
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost("smtp.gmail.com");
            mailSender.setPort(Integer.parseInt(Objects.requireNonNull("587")));
            mailSender.setUsername("~my email~");
            mailSender.setPassword("~my pw~");

            Properties javaMailProperties = new Properties();
            javaMailProperties.put("mail.smtp.starttls.enable", "true");
            javaMailProperties.put("mail.smtp.auth", "true");
            javaMailProperties.put("mail.transport.protocol", "smtp");
            javaMailProperties.put("mail.debug", "true");
            javaMailProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

            mailSender.setJavaMailProperties(javaMailProperties);

            sendEmailAndUpdate(reciever, pdf, mailSender);

            System.out.println("Email Sent  Successfully...");

        } catch (Exception e) {
            System.out.println("EmailService File Error" + e);
            return false;
        }
        return true;

    }

现在,在 sendEmailAndUpdate 方法中,我有收件人的电子邮件地址、pdf(作为 MultipartFile)和 JavaMailSender。到目前为止,这是这个方法:

private void sendEmailAndUpdate(String recieverEmail, MultipartFile file, JavaMailSender mailSender) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

            mimeMessageHelper.setSubject("PDF email");
            mimeMessageHelper.setFrom(~My Email~);
            mimeMessageHelper.setTo(recieverEmail);
            
            mimeMessageHelper.setText("This is email body");

            // Code for attaching the PDF goes here

            mailSender.send(mimeMessageHelper.getMimeMessage());

        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

在这里,我想将 pdf 文件(我有一个 MultipartFile)附加到电子邮件中。这可能是一个菜鸟问题,我可能遗漏了一些明显的东西,但我是新手,我在网上找不到任何关于如何做到这一点的资源。任何人都可以 link 我找到这样的资源或提供解决方案吗?提前谢谢你。

可以直接附上

 mimeMessageHelper.addAttachment("fileName", file);

MultipartFile 已经扩展 class of InputStreamSource

 public interface MultipartFile extends InputStreamSource {