1
    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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.