Search code examples
spring-bootjakarta-mail

How to copy email in sent folder?


I am using java mail api for sending mail.In that for sending email i configured yahoo smtp port.I am able to send mail from yahoo account but sent mail is not saving in sent item.For incoming service i configured imap yahoo server.

service:

 @Component
    public class SmtpMailSender {   
        @Autowired
        private JavaMailSender javaMailSender;
        private static String folderName = "Sent";

        private String host="smtp.mail.yahoo.com";
        private String user="[email protected]";
        private String pass="xxxx";

        public void send(String to,String subject,String body, String from) throws MessagingException
        {
            // Java Mail properties
            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.auth", "true");

            // Mail session authentified
            Session session = Session.getInstance(props);

            MimeMessage message=javaMailSender.createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(message,true);

            helper.setTo(to); 
            helper.setFrom(from);
            helper.setSubject(subject);  
            helper.setText(body,true);
            javaMailSender.send(message);

            // Copy message to "Sent Items" folder as read
            Store store = session.getStore();
            store.connect("imap.mail.yahoo.com", user, pass);

            Folder folder = store.getFolder(folderName);
            if (!folder.exists()) {
                folder.create(Folder.HOLDS_MESSAGES);
            }
            folder.open(Folder.READ_WRITE);
            folder.appendMessages(new Message[] {message});
            message.setFlag(FLAGS.Flag.RECENT, true);
            System.out.println("Msg send and saved ....");
            store.close();
        }
    }

Controller:

 @RestController
    public class EmailController {
        @Autowired private SmtpMailSender smtpMailSenderObj;
        @RequestMapping("/send")
        public void sendMail() throws MessagingException {
            smtpMailSenderObj
                .send
                    ("[email protected]", "verify sendMail",
                    "Hii...this is demo for java email send",
                            "[email protected]");
            }
    }

Application.properties:

spring.mail.host=smtp.mail.yahoo.com
[email protected]
spring.mail.password=xxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.transport.protocol : smtp
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.socketfactory.port=465
spring.mail.properties.mail.imap.ssl.required=true
spring.mail.properties.mail.imap.port=993

Solution

  • This is because of imap port connection send mail was not saving in sent items. Need to add set property in properties file.

    props.setProperty("mail.store.protocol", "imaps");
    

    By adding this now i am able to save my sent mail in sent item.