package org.fastdev.restmailsender;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

@SpringBootApplication
public class RestMailSenderApplication {

    private final Logger log = LoggerFactory.getLogger(RestMailSenderApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(RestMailSenderApplication.class, args);
    }

    @RestController
    @RequestMapping("/v1/send-mail")
    public class SendMail {

        @Autowired
        private JavaMailSender sender;

        @Value("${rest.mail.from}")
        String mailFrom;


        @PostMapping("/with-attachment")
        public String mailWithAttachment(@RequestBody MailTo mailTo) {
            try {
                // SERA
                log.info("Send mail with parameters MailTo= {}", mailTo);
                sendEmail(mailTo);
                return "{\"message\": \"OK\"}";
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return "{\"message\": \"Error\"}";
            }
        }

        private void sendEmail(MailTo mailTo) throws Exception {
//            MimeMessage message = sender.createMimeMessage();
//            MimeMessageHelper helper = new MimeMessageHelper(message);
//            helper.setFrom(mailFrom);
//            helper.setTo(mailTo.getTo());//"lasalazar@gmail.com");
//            helper.setSubject(mailTo.getSubject());//"Hi");
//            helper.setText(mailTo.getMessage());//"How are you?");
//
//            sender.send(message);

            //
            MimeMessage mail = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);
            helper.setTo(mailTo.getTo());
            helper.setSubject(mailTo.getSubject());
            helper.setText(mailTo.getMessage(), true);
            helper.setFrom(mailFrom);
            ClassPathResource file = new ClassPathResource("sample.png");
            helper.addAttachment("sample.png", file);

            sender.send(mail);


//
//            if (emailVo.getImage() != null) {
//                byte[] imageByte = Base64.getDecoder().decode(emailVo.getImage());
//                helper.addAttachment(emailVo.getImageName(), new ByteArrayResource(imageByte));
//            }
            //
        }
    }
}
@@ -333,62 +333,8 @@
 on;%0A
-import org.springframework.core.io.ByteArrayResource;%0A
 impo
@@ -383,16 +383,16 @@
 source;%0A
+
 import o
@@ -791,195 +791,9 @@
 ge;%0A
-import java.io.BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileReader;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.Base64;
 %0A
+
 %0A@Sp
@@ -1408,32 +1408,8 @@
 y %7B%0A
-                // SERA%0A
     
@@ -1788,440 +1788,8 @@
 n %7B%0A
-//            MimeMessage message = sender.createMimeMessage();%0A//            MimeMessageHelper helper = new MimeMessageHelper(message);%0A//            helper.setFrom(mailFrom);%0A//            helper.setTo(mailTo.getTo());//%22lasalazar@gmail.com%22);%0A//            helper.setSubject(mailTo.getSubject());//%22Hi%22);%0A//            helper.setText(mailTo.getMessage());//%22How are you?%22);%0A//%0A//            sender.send(message);%0A%0A            //%0A
     
@@ -2241,24 +2241,26 @@
             
+//
 sender.send(
@@ -2270,275 +2270,8 @@
 l);%0A
-%0A%0A//%0A//            if (emailVo.getImage() != null) %7B%0A//                byte%5B%5D imageByte = Base64.getDecoder().decode(emailVo.getImage());%0A//                helper.addAttachment(emailVo.getImageName(), new ByteArrayResource(imageByte));%0A//            %7D%0A            //%0A
     

Clean Code