Home > OS >  Adding multipart file as an attachment to an email in Spring Boot
Adding multipart file as an attachment to an email in Spring Boot

Time:05-12

I want to send an email from Spring Boot with a pdf attachment. I have received the pdf file as a multipart file from a POST call.

Here's my controller class so far (sendEmails method is included in emailService service):

@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:

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. 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. 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.

CodePudding user response:

You can attached directly

 mimeMessageHelper.addAttachment("fileName", file);

MultipartFile already extend class of InputStreamSource

 public interface MultipartFile extends InputStreamSource {
  • Related