在java中使用IMAP阅读电子邮件

时间:2016-02-10 05:10:15

标签: java javamail imap

我正在尝试使用java中的IMAP从我的收件箱中读取电子邮件,但我收到了java.net.UnknownHostException。这是与代理还是防火墙相关的东西?当我第一次做这样的节目时,我很困惑。请帮忙。提前谢谢。

public class mailRead {

   public static void check(String host, String storeType, String user, String password) 
   {
      try {

          Properties props = new Properties();

        props.setProperty("mail.store.protocol", "imaps");
            Session session = Session.getInstance(props, null);
            Store store = session.getStore("imaps");

            System.out.println("emailsession done !!");


            store.connect("2e1211jk-1178@external.pk.com", "raj21@pk.com", "kkk111KKK");


          //create the folder object and open it
          Folder emailFolder = store.getFolder("INBOX");
          emailFolder.open(Folder.READ_ONLY);

          Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);

            Message msg = inbox.getMessage(inbox.getMessageCount());
            Address[] in = msg.getFrom();

            for (Address address : in) {
                System.out.println("FROM:" + address.toString());
            }



      store.close();

      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {

      String host = "2e1211jk-1178@external.pk.com";// change accordingly
      String mailStoreType = "IMAP";
      String username = "raj21@pk.com";// change accordingly
      String password = "kkk111KKK";// change accordingly

      check(host, mailStoreType, username, password);

   }

}

1 个答案:

答案 0 :(得分:0)

Store#connect(...)中的第一个参数是主机名。您传递的电子邮件地址不是有效的主机名。