运行Selenium时JavaMail出错

时间:2014-09-25 19:07:35

标签: javamail

我正在尝试提取运行selenium和maven的电子邮件,但在尝试连接时出现以下错误:

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

但是当我在不使用selenium的不同项目中运行完全相同的代码时,它是否有效,任何想法导致了什么?

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

1 个答案:

答案 0 :(得分:0)

那是因为没有名为“pop.google.com”的主机。也许你的意思是“pop.gmail.com”?

由于first common mistake described in this JavaMail FAQ entry,它可能适用于某些机器。