sb-0205
sb-0205

Reputation: 1

How can I add MultipartFile from request to an object of MimeMessageHelper via addAttachment( )?

following is my controller portion

@RequestMapping(value = "/message-for-support/create", method = RequestMethod.POST)
    public ResponseEntity<?> create(@RequestParam("subject") @NotEmpty @NotNull String subject,
                                    @RequestParam("detail") @NotEmpty @NotNull String detail,
                                    @RequestParam("attachmentFile") MultipartFile attachmentFile) {
        return supportSideService.createSupportSideMessage(subject, detail, attachmentFile);
    }

following two are the methods of service class

public ResponseEntity<?> createSupportSideMessage(String subject, String detail, MultipartFile attachmentFile) {
        SupportSide supportSide = new SupportSide();
        try {
            supportSide.setSubject(subject);
            supportSide.setDetail(detail);
            sendSimpleMailMessage(subject, detail, attachmentFile);
        } catch (Exception e) {
            return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        supportSideRepository.save(supportSide);
        return new ResponseEntity<>("message sent!!!!", HttpStatus.OK);
    }
@Async
    public void sendSimpleMailMessage(String subject, String detail, MultipartFile attachmentFile) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setSubject(subject);
            helper.setFrom(fromEmail);
            helper.setTo(to);
            helper.addAttachment(attachmentFile.getOriginalFilename(), attachmentFile);
            javaMailSender.send(message);
        } catch (Exception exception) {
            throw new RuntimeException(exception.getMessage());
        }
    }

I followed the following link to try this, but it does not seem to work text

I tried to attach the MultipartFile from request header to email.

I'm expecting to attach the MultipartFile from request header to email.

Upvotes: 0

Views: 25

Answers (0)

Related Questions