Java: using Javamail to read email

I am working on a program that, let's say, uses javamail to read in the inbox, and if the email contains a specific string, it should be opened.

Firstly this is my first time trying to use this javamail api. I'm sure my properties are all tainted. Can anyone give me some advice on how to properly configure javamail properties?

Second, I feel like I can connect via the API, but if I try to find topics and the topic is null, I get a null pointer exception. If I, however, do not try to match the theme to "Ordretest", I am not getting nullpointer - any advice would be of 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", "myEmail@live.dk", "MyPassword");
                session.setDebug(true);
                Folder inbox = store.getFolder("INBOX");
                inbox.open(Folder.READ_ONLY);


                SearchTerm sender = new FromTerm(new InternetAddress("myMail@live.dk"));

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

      

+3


source to share


2 answers


Not knowing which line you are getting NPE (Null Pointer Exception) on, I assume it is happening here:

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

      



If getSubject () returns null and you try to execute .equals () it will call NPE (because you are trying to call the method). So try rewriting it (if your message object cannot be null):

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

      

+1


source


Where to begin...

Did you find the JavaMail FAQ ?



You can get rid of all property settings because they don't do anything since you are using the "imaps" protocol and not the "pop3" protocol.

As others have explained, the getSubject method can return null, so you need to check for that.

0


source







All Articles