Search code examples
jakarta-mail

Error in JavaMail when running Selenium


I'm trying to extract an email running selenium and maven but I get the following error when trying to connect:

javax.mail.MessagingException: Connect failed;
  nested exception is:
    java.net.UnknownHostException: pop.google.com
    at com.sun.mail.pop3.POP3Store.protocolConnect

But when I run the same exact code in a different project without selenium it works, any idea what's causing this?

public class EmailService {

    private static String SERVER;
    private static String USER;
    private static String PASSWORD;
    private static final String TEXT_FROM="SMS from";


    /**
     * Constructor to setup imap info
     * @param server email server to connect
     * @param usr email address of user
     * @param passwd password of user
     */
    public EmailService(String server, String usr, String passwd){
        SERVER = server;
        USER = usr;
        PASSWORD = passwd;
    }


    public static String receive(String type, String receiver, Date d) {
        Store store = null;
        Folder folder= null;
        SubjectTerm subject;
        RecipientStringTerm recipient = new RecipientStringTerm(Message.RecipientType.TO,USER);

        try{
            //Get session
            Properties props = new Properties();
            //props.setProperty("mail.store.protocol","imaps");
            props.put("mail.pop3.host", SERVER);
            props.put("mail.pop3.port", "995");
            props.put("mail.pop3.starttls.enable", "true");
            //Session session = Session.getInstance(props, null);
            Session session = Session.getDefaultInstance(props);

            //Get  message store and connect
            store = session.getStore("pop3s");
            store.connect(SERVER,USER,PASSWORD);

            //Get default folder
            folder = store.getDefaultFolder();
            if(folder == null) throw new Exception("No Default folder");

            //Get Inbox
            folder = folder.getFolder("INBOX");
            if(folder == null) throw new Exception ("No Inbox");

            //Open folder for read only
            folder.open(Folder.READ_ONLY);

Solution

  • That's because there is no host named "pop.google.com". Maybe you meant "pop.gmail.com"?

    It probably works on some machines because of the first common mistake described in this JavaMail FAQ entry.