Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Using Javamail to read email

I've been working on a program, that's suppose to use javamail, to read through an inbox, and if an email contains a specific String, it should be opened.

First off, this is my first time trying to use this javamail api - I'm pretty sure that my properties are all messed up - Can anyone give me at tip on correctly setting up properties for javamail?

Secondly, it seems like i can connect via the API, but if I try to search for subjects AND the subject is null, I get a null pointer exception - If I however don't try to match the subject with "Ordretest", I get no nullpointer - Any tips would be a great help :)

package vildereMail;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.search.FromTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;

public class vildereMail {


    public boolean match(Message message) {
        try {
            if (message.getSubject().contains("Ordretest")) {
                System.out.println("match found");
                return true;
            }
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
        return false;
    };
        public static void main(String[] args) {
            Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
            props.put("mail.imap-mail.outlook.com.ssl.enable", "true");
            props.put("mail.pop3.host", "outlook.com");
            props.put("mail.pop3.port", "995");
            props.put("mail.pop3.starttls.enable", "true");
            try {
                Session session = Session.getInstance(props, null);
                Store store = session.getStore();
                store.connect("imap-mail.outlook.com", "[email protected]", "MyPassword");
                session.setDebug(true);
                Folder inbox = store.getFolder("INBOX");
                inbox.open(Folder.READ_ONLY);


                SearchTerm sender = new FromTerm(new InternetAddress("[email protected]"));

                Message[] messages = inbox.search(sender);
                System.out.println(messages);

                for (int i = 0 ; i < messages.length ; i++) {

                    System.out.println(messages[i].getSubject());
                    if (messages[i].getSubject().equals(null)) {
                        System.out.println("null in subject");
                        break;
                    }
                    else if (messages[i].getSubject().contains("Ordretest")){
                        System.out.println("1 match found");
                    }
                    else {
                    System.out.println("no match");
                    }
                }
                System.out.println("no more messages");
                store.close();

            } catch (Exception mex) {
                mex.printStackTrace();
            }
        }
    }
like image 210
Jesper Avatar asked May 19 '15 07:05

Jesper


People also ask

How can I get SMTP server response using JavaMail?

SMTPTransport t = (SMTPTransport)session. getTransport("smtps"); t. send(message); String response = t. getLastServerResponse(); boolean s = t.

Which protocol is used to receive the messages in JavaMail?

IMAP: Acronym for Internet Message Access Protocol. It is an advanced protocol for receiving messages. It provides support for multiple mailbox for each user, in addition to, mailbox can be shared by multiple users. It is defined in RFC 2060.


1 Answers

Without knowing on which line you get the NPE (Null Pointer Exception) I guess it occurs here:

if (messages[i].getSubject().equals(null)) 

If getSubject() returns null and you try to do .equals() it will throw a NPE (because you try to invoke a method). So try to rewrite it to (assuming your message object can't be null):

if (messages[i].getSubject() == null) 
like image 82
User404 Avatar answered Nov 15 '22 07:11

User404