2011-03-12 12 views
6

मैं जावा में शुरुआती हूं और हर रोज मुझे संलग्न फाइलों वाली मशीन से कई ईमेल प्राप्त होते हैं। मुझे हमेशा के लिए दृष्टिकोण खोलना है और संलग्न फ़ाइल को पुनर्प्राप्त करना है और फिर उसे एक फ़ोल्डर में रखना है।जावमेल के साथ दृष्टिकोण के ईमेल को कैसे पढ़ा जाए?

जावा में या जावा में किसी और चीज़ के साथ यह कैसे कर सकता है, इसे ईमेल खोलना होगा, संलग्न xtt फ़ाइल को पुनर्प्राप्त करना होगा और फिर उसे फ़ोल्डर में संग्रहीत करना होगा।

क्या कोई मुझे यह कार्य करने में मदद कर सकता है या मुझे ट्यूटोरियल पेज या नमूना पर निर्देशित कर सकता है।

आपको बहुत बहुत

+1

यह उत्पादन लोगों के लिए वास्तविक समय की समस्या हो सकती है! – Jayy

उत्तर

0

धन्यवाद शायद तुम procmail की तरह कुछ का उपयोग कर तरह संदेश प्रदर्शन कर सकता है? यह भी सुनिश्चित करें कि आप pop3 के बजाय imap का उपयोग कर रहे हैं। यह आपको सर्वर के पक्ष में फ़ोल्डर्स में अपने ई-मेल व्यवस्थित करने की क्षमता देगा।

+0

ठीक है, मैं कैसे जान सकता हूं कि यह दृष्टिकोण में imap या pop3 है? – user618111

9

आप java mail का उपयोग करके ऐसा कर सकते हैं। आपको कॉन्फ़िगरेशन विवरण ढूंढने की आवश्यकता होगी लेकिन इसके लिए मानक कोड स्निपेट नीचे जैसा कुछ होगा। मैंने here से कोड को कॉपी किया। Thos आधिकारिक javamail link उदाहरणों का एक सुंदर सभ्य सेट है (यानी संलग्नक कैसे पढ़ा जाए)।

किसी फ़ोल्डर में फ़ाइल के रूप में ईमेल संग्रहीत करने के लिए, आप apache FileUtils कर सकते हैं। एक फ़ाइल को ईमेल लिखें और उसे अपनी इच्छित फ़ोल्डर में कॉपी करें।

एचटीएच!

 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

स्टोर स्टोर = session.getStore ("imaps"); getStore() विधि में तर्क के साथ बहुत महत्वपूर्ण है, आपके कोड के लिए धन्यवाद। – janwen

+0

@janwen अरे दोस्तों, हे, समस्या हल हो गई? क्या आप यहां मेरी मदद कर सकते हैं (stackoverflow.com/q/36483341/3703397)? – Marin

0

मैंने इस अंतहीन समस्या को हल किया है।

नोटिस:

  • मैं IMAP प्रोटोकॉल
  • मैं केवल कनेक्शन का उपयोग और इस वर्ग का उपयोग कर रहा ईमेल मैं

प्राप्त हुआ है मैं गुण इस के इन सेट के साथ आशा है कि देखने के लिए उन लोगों की मदद कर सकता है जिन्होंने इसे पढ़ने, लिखने, जो भी ईमेल बल्कि दृष्टिकोण के साथ संघर्ष किया है।

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(); 
    } 
} 
संबंधित मुद्दे