title: How to copy email in sent folder?
body: <p>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.</p>
  
  <p><strong>service:</strong></p>
  
  <pre><code> @Component
      public class SmtpMailSender {   
          @Autowired
          private JavaMailSender javaMailSender;
          private static String folderName = "Sent";
  
          private String host="smtp.mail.yahoo.com";
          private String user="abc@yahoo.com";
          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();
          }
      }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code> @RestController
      public class EmailController {
          @Autowired private SmtpMailSender smtpMailSenderObj;
          @RequestMapping("/send")
          public void sendMail() throws MessagingException {
              smtpMailSenderObj
                  .send
                      ("pqr@gmail.com", "verify sendMail",
                      "Hii...this is demo for java email send",
                              "abc@yahoo.com");
              }
      }
  </code></pre>
  
  <p><strong>Application.properties:</strong></p>
  
  <pre><code>spring.mail.host=smtp.mail.yahoo.com
  spring.mail.username=abc@yahoo.com
  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
  </code></pre>