2

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

Snake
  • 14,228
  • 27
  • 117
  • 250
  • 1
    You are confusing several different functions of email protocols. Saving the email in your sent folder will require an IMAP client. Sending the email only requires SMTP. – Elliott Frisch Jun 02 '16 at 23:08
  • I thought IMAP is for retreiving emails. Is it a different process to save email in Sent folder? I thought when email is sent then email provider saves the email automatically ( appernetly not) – Snake Jun 02 '16 at 23:31
  • 1
    I already answered a similar question [here](http://stackoverflow.com/questions/37518023/messages-sent-via-yahoo-using-javamail-api-not-going-to-sent-messages-folder/37518870#37518870). – Bill Shannon Jun 02 '16 at 23:40
  • SMTP is for sending (and receiving) email. Storing email in a *folder* (even the "sent" folder), is performed with an IMAP client. SMTP doesn't have a concept of "folders", it's fire and forget. – Elliott Frisch Jun 03 '16 at 00:07
  • @BillShannon This makes sense. Thanks for the reference. I wish there is example using Apache common – Snake Jun 03 '16 at 10:04
  • This is both the bug and the feature of Apache Commons Email - it's simple because it's only trying to handle the very limited case of sending email. Once you step out of that use case, you need to understand how to use JavaMail directly. Once you understand how to use JavaMail directly, using it to send email is not that difficult. – Bill Shannon Jun 03 '16 at 22:50
  • Thank you. Feel free to put ur comment as an answer so I can accept it. Unless someone can put scripted code :) – Snake Jun 04 '16 at 17:50

1 Answers1

1

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

davey
  • 1,666
  • 17
  • 24