2011-03-12 73 views
6

我是java的初學者,每天都會收到來自機器附帶文件的許多電子郵件。 我必須打開每天的展望和檢索附加的文件,然後把它放在一個文件夾中。如何用javamail閱讀Outlook的電子郵件?

如何用javamail或java中的其他東西做到這一點,它必須打開電子郵件,檢索附加的xtt文件,然後將其存儲在一個文件夾中。

有人可以幫我做這個任務或指導我進入教程頁面或樣本。

非常感謝您

+1

這可能是生產傢伙的實時問題! – Jayy 2012-03-02 13:07:53

回答

0

也許你可以像procmail執行排序消息使用的東西?另外請確保您使用的是imap而不是pop3。它將使您能夠將電子郵件組織到服務器端的文件夾中。

+0

好的我怎麼知道它是imap還是pop3在outlook? – user618111 2011-03-12 21:58:10

9

你可以使用java mail來做到這一點。你需要找到配置細節,但是這個標準的代碼片段就像下面這樣。我複製了從here截取的代碼。官方javamail link有一套相當不錯的例子(即如何閱讀附件)。

用於將電子郵件作爲文件存儲到文件夾,您可以apache FileUtils。將電子郵件寫入文件並將其複製到您需要的文件夾中。

HTH!

 Properties props = System.getProperties(); 
    props.setProperty("mail.store.protocol", "imaps"); 
    Session session = Session.getDefaultInstance(props, null); 
    Store store = session.getStore("imaps"); 
    store.connect("<impap_address>","<mail ID> ", "<Password>"); 

    inbox = store.getFolder("Inbox"); 
    System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount()); 
    inbox.open(Folder.READ_ONLY); 

    /* Get the messages which is unread in the Inbox*/ 
    Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); 
+0

Store store = session.getStore(「imaps」); getStore()方法中的參數非常重要,感謝您的代碼。 – janwen 2014-04-20 07:28:00

+0

@janwen嘿,夥計們,嘿,解決了這個問題?你能幫我嗎(stackoverflow.com/q/36483341/3703397)? – Marin 2016-04-07 22:14:43

0

我已經用這種方法解決了這個無窮無盡的問題。

注意:

  • 我使用的IMAP協議
  • 我只用連接和這個類來看看我收到

的電子郵件,我希望能與這些設置屬性的可以幫助很多與此有關的人閱讀,撰寫,無論是電子郵件,還是Outlook。

public class OutlookReader_imap { 

    Folder inbox; 

    // Constructor of the calss. 

    public OutlookReader_imap() { 
     System.out.println("Inside MailReader()..."); 
     final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

     /* Set the mail properties */ 
     /* 
     props.put("mail.smtp.starttls.enable", "true"); 
     Session session = Session.getInstance(props); 
     MimeMessage msg = new MimeMessage(session); 
     // set the message content here 
     Transport.send(msg, username, password); 
     */ 


     Properties props = System.getProperties(); 
     // Set manual Properties 
     props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY); 
     props.setProperty("mail.imaps.socketFactory.fallback", "false"); 
     props.setProperty("mail.imaps.port", "993"); 
     props.setProperty("mail.imaps.socketFactory.port", "993"); 
     props.put("mail.imaps.host", "imap-mail.outlook.com"); 


     try { 
      /* Create the session and get the store for read the mail. */ 

      Session session = Session.getDefaultInstance(System.getProperties(), null); 
      Store store = session.getStore("imaps"); 

      store.connect("imap-mail.outlook.com", 993, "<email>", "<password>"); 

      /* Mention the folder name which you want to read. */ 

      // inbox = store.getDefaultFolder(); 
      // inbox = inbox.getFolder("INBOX"); 
      inbox = store.getFolder("INBOX"); 

      /* Open the inbox using store. */ 

      inbox.open(Folder.READ_ONLY); 

      Message messages[] = inbox.search(new FlagTerm(new Flags(
        Flags.Flag.SEEN), false)); 
      System.out.println("No. of Unread Messages : " + inbox.getUnreadMessageCount()); 

      /* Use a suitable FetchProfile */ 
      FetchProfile fp = new FetchProfile(); 
      fp.add(FetchProfile.Item.ENVELOPE); 

      inbox.fetch(messages, fp); 

      try { 

       printAllMessages(messages); 

       inbox.close(true); 
       store.close(); 

      } catch (Exception ex) { 
       System.out.println("Exception arise at the time of read mail"); 
       ex.printStackTrace(); 
      } 

     } catch (MessagingException e) { 
      System.out.println("Exception while connecting to server: " + e.getLocalizedMessage()); 
      e.printStackTrace(); 
      System.exit(2); 
     } 

    } 

    public void printAllMessages(Message[] msgs) throws Exception { 
     for (int i = 0; i < msgs.length; i++) { 
      System.out.println("MESSAGE #" + (i + 1) + ":"); 
      printEnvelope(msgs[i]); 
     } 
    } 

    public void printEnvelope(Message message) throws Exception { 

     Address[] a; 

     // FROM 
     if ((a = message.getFrom()) != null) { 
      for (int j = 0; j < a.length; j++) { 
       System.out.println("De : " + a[j].toString()); 
      } 
     } 

     String subject = message.getSubject(); 

     Date receivedDate = message.getReceivedDate(); 
     Date sentDate = message.getSentDate(); // receivedDate is returning 
     // null. So used getSentDate() 

     //Dar Formato a la fecha 
     SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 

     System.out.println("Asunto : " + subject); 

     if (receivedDate != null) { 
      System.out.println("Recibido: " + df.format(receivedDate)); 
     } 

     System.out.println("Enviado : " + df.format(sentDate)); 
    } 


    public static void main(String args[]) { 
     new OutlookReader_imap(); 
    } 
}