Sami
Sami

Reputation: 8419

Read sent mails using smtp java

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();

Upvotes: 0

Views: 12484

Answers (3)

Deepanjan
Deepanjan

Reputation: 649

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.)

Upvotes: 0

Sylvain Leroy
Sylvain Leroy

Reputation: 11

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.

Upvotes: 0

Bill Shannon
Bill Shannon

Reputation: 29971

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.

Upvotes: 2

Related Questions