javajakarta-mailapache-commons-email

Sending email using Apache mail is not saved in the sent folder


I am using Apache Commons Mail library to send email (using their simple SMTP email example).

The email is sent using one of the famous providers (i used yahoo as an example). The email was sent successfully. However when I login to my yahoo account I don't see the email in the sent folder.

Is there a flag I need to enable or some other thing I need to code to ensure that email is being saved in the sent folder?

Please assist. Thank you


Solution

  • i just had the same issue an solved it by:

        ...
        // send the org.apache.commons.mail.HtmlEmail
        email.send();
        copyIntoSent(email.getMailSession(), email.getMimeMessage());
    }
    
    private void copyIntoSent(final Session session, final Message msg) throws MessagingException
    {
        final Store store = session.getStore("imaps");
        store.connect(IMAP_HOST, SMTP_AUTH_USER, SMTP_AUTH_PWD);
    
        final Folder folder = (Folder) store.getFolder("Sent Items");
        if (folder.exists() == false) {
            folder.create(Folder.HOLDS_MESSAGES);
        }
        folder.open(Folder.READ_WRITE);
    
        folder.appendMessages(new Message[] { msg });
    }
    

    Note that you must use the imap-host here, not the smtp-host. The difference about these protocols should be clear.

    With kind regards

    davey