Jacku
Jacku

Reputation: 39

java.io.FileNotFoundException: (The system cannot find the file specified) when using multipart

    package com.act.webmail.service;

    import javax.mail.internet.MimeMessage;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.io.ByteArrayResource;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;

    import com.act.webmail.dto.MessageToSend;

    @Service
    public class ActMailSenderService {

        @Autowired
        private JavaMailSender javaMailSender;

        @Async
        public void sendEmail(MessageToSend messageToSend, MultipartFile... multipartFileList) {

            try {
                MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
                if (messageToSend.getReplyTo() != null && !messageToSend.getReplyTo().equals("")){
                    mimeMessage.addHeader("In-Reply-To", messageToSend.getReplyTo());
                }
                helper.setFrom("[email protected]");
                helper.setTo(messageToSend.getTo());
                helper.setSubject(messageToSend.getSubject());
                helper.setText(messageToSend.getBody(), true);
                for (MultipartFile multipartFile : multipartFileList) {
                    byte[] multipartFileByteArray= multipartFile.getBytes();
                    helper.addAttachment(multipartFile.getOriginalFilename(), new ByteArrayResource(multipartFileByteArray));   
                }
                javaMailSender.send(mimeMessage);
                System.out.println(messageToSend.getReplyTo() + " sent successfully!");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

I initialised new ByteArrayResource(multipartFile.getBytes()) and used in addAttachment function of org.springframework.mail.javamail.MimeMessageHelper but i'm getting an exception "java.io.FileNotFoundException: C:\Users\Jackson Baby\AppData\Local\Temp\tomcat.8088819519816892725.8080\work\Tomcat\localhost\ROOT\upload_525fd01b_db90_4589_921f_50bf9a1e6e47_00000001.tmp (The system cannot find the file specified)"

is there any way to solve this issue?

Upvotes: 1

Views: 1606

Answers (1)

A_H
A_H

Reputation: 121

I had the same issue where when I would try to the get the bytes or input stream of the file; I would be getting File Not Found Exception. I finally solved it by removing the @Async Annotation from the Rest Controller Method. That seemed to be creating the issue...

Upvotes: 2

Related Questions