简体   繁体   English

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

[英]Adding multipart file as an attachment to an email in Spring Boot

I want to send an email from Spring Boot with a pdf attachment.我想从 Spring Boot 发送一封带有 pdf 附件的电子邮件。 I have received the pdf file as a multipart file from a POST call.我从 POST 调用中收到了作为多部分文件的 pdf 文件。

Here's my controller class so far ( sendEmails method is included in emailService service):到目前为止,这是我的控制器类( 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");
        }
    }

And here's sendEmails method:这是 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;

    }

Now, in the sendEmailAndUpdate method I have the reciever's email address, the pdf (as a MultipartFile), and the JavaMailSender.现在,在sendEmailAndUpdate方法中,我有收件人的电子邮件地址、pdf(作为 MultipartFile)和 JavaMailSender。 Here's this method so far:到目前为止,这是这个方法:

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();
        }

    }

Here I want to attach the pdf file (which I have as a MultipartFile) to the email.在这里,我想将 pdf 文件(我有一个 MultipartFile)附加到电子邮件中。 This might be a noob question and I might be missing something obvious, but I'm new to this and I couldn't find any resources online on how to do this.这可能是一个菜鸟问题,我可能会遗漏一些明显的东西,但我是新手,我在网上找不到任何关于如何做到这一点的资源。 Can anyone link me to such resource or provide with a solution?任何人都可以将我链接到此类资源或提供解决方案吗? Thank you in advance.先感谢您。

You can attached directly可以直接贴

 mimeMessageHelper.addAttachment("fileName", file);

MultipartFile already extend class of InputStreamSource MultipartFile已经扩展了InputStreamSource

 public interface MultipartFile extends InputStreamSource {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM