0

I have been trying lot of things from multiple resources like Read Inbox Java2s and How to get the list of available folders in a mail account using JavaMail

I am sending emails succesfully, but to be sure of mail is sent successfully, i need to read emails from sent items folder Is this possible with smtp? if yes, how?

Currently I am stuck even in connceting with stroe. I am finding no way to pass the steps Store store = session.getStore(); and store.connect();

I have no idea about imap or pop3. They might not have been even configured at our server, but if smtp does not faicilitate then I am ready to process with those protocols, though I am sending mails using stmp yet. I have tried lot of edits in my following code, but nothing is helping

        String host = "mysite.smtp.com";
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "myport");
        props.put("mail.smtp.auth", "true");


        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                String user = "myname";
                String password = "mypassword";
                return new PasswordAuthentication(user, password);
            }
        });
        Store store = session.getStore(); // had tried writing "imaps" here
        store.connect(host, null, null);
        //store.connect(); also tried this

        Folder inbox = store.getFolder("INBOX"); //actually i need "SENT"
        if (inbox == null) {
          System.out.println("No INBOX");
          System.exit(1);
        }
        inbox.open(Folder.READ_ONLY);

        Message[] messages = inbox.getMessages();
        for (int i = 0; i < messages.length; i++) {
          System.out.println("Message " + (i + 1));
          messages[i].writeTo(System.out);
        }
        inbox.close(false);
        store.close();
Community
  • 1
  • 1
Sami
  • 8,168
  • 9
  • 66
  • 99
  • Refer this [link](https://stackoverflow.com/questions/14730384/how-to-save-sent-items-mail-using-the-java-mail-api/48658249#48658249) Save Sent mail using JAVA MAIL API – Deepanjan Feb 07 '18 at 08:58

3 Answers3

2

You can't use smtp to read email, you need to use imap. pop3 won't help because it only lets you read the Inbox.

Depending on what you mean by "sent successfully", a successful return from the send method will tell you what you want to know.

Note also that depending on the mail server you're using, sent messages don't automatically appear in a "Sent Messages" folder; you might need to copy them their yourself after sending the message. (Gmail does this automatically, but not all other servers do.)

If what you really want to know is that the message was successfully delivered to the destination mail server(s) and all the addresses were valid, that's harder. The JavaMail FAQ has more information.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
0

I know that I am answering late. I encountered quite similar problems while maintaining a software using extensively mail sending functionalities. I finally created a JUnit extension to write integration tests with a SMTP server emulation.

Please have a look to github.com/sleroy/fakesmtp-junit-runner/. By the way it's open-source.

0

Refer this link:

How to save sent items mail using the Java mail API?

Sent Mail Service using JAVAMAIL API

Note also that depending on the mail server you're using, sent messages don't automatically appear in a "Sent Messages" folder; you might need to copy them their yourself after sending the message. (Gmail does this automatically, but not all other servers do.)

Deepanjan
  • 649
  • 2
  • 8
  • 15