Accessing emails from gmail using IMAP ( javamail API)

后端 未结 5 1739
無奈伤痛
無奈伤痛 2020-12-13 04:20

I am trying to access emails from Gmail accounts through IMAP with the help of the JavaMail API. I was wondering why the code works for one email account but doesn\'t work f

相关标签:
5条回答
  • 2020-12-13 05:02

    Im unsure if this helps, but I've seen instances where gmail accounts have different mailboxes ie..

    Gmail Account 1 :-

    [[Google Mail]]
    [[Google Mail]/All Mail]
    [[Google Mail]/Bin]
    [[Google Mail]/Drafts]
    [[Google Mail]/Important]
    [[Google Mail]/Sent Mail]
    [[Google Mail]/Spam]
    [[Google Mail]/Starred]
    

    Gmail Account 2 :-

    [[Gmail]]
    [[Gmail]/All Mail]
    [[Gmail]/Bin]
    [[Gmail]/Drafts]
    [[Gmail]/Important]
    [[Gmail]/Sent Mail]
    [[Gmail]/Spam]
    [[Gmail]/Starred]
    
    0 讨论(0)
  • Is one of the accounts using non-english UI by any chance?

    Gmail folder names are localized with respect to the user localization settings.

    Currently the only way to get the name of the localized folder is by using XLIST command.

    0 讨论(0)
  • 2020-12-13 05:06
    package com.technicalkeeda;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.Properties;
    
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Store;
    
    public class GmailInbox {
    
     public static void main(String[] args) {
      GmailInbox gmail = new GmailInbox();
      gmail.read();
     }
    
     public void read() {
      Properties props = new Properties();
      try {
       props.load(new FileInputStream(new File("C:\\smtp.properties")));
       Session session = Session.getDefaultInstance(props, null);
    
       Store store = session.getStore("imaps");
       store.connect("smtp.gmail.com", "*************@gmail.com","your_password");
    
       Folder inbox = store.getFolder("inbox");
       inbox.open(Folder.READ_ONLY);
       int messageCount = inbox.getMessageCount();
    
       System.out.println("Total Messages:- " + messageCount);
    
       Message[] messages = inbox.getMessages();
       System.out.println("------------------------------");
       for (int i = 0; i < 10; i++) {
          System.out.println("Mail Subject:- " + messages[i].getSubject());      
       }
       inbox.close(true);
       store.close();
    
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    
    }
    
    0 讨论(0)
  • 2020-12-13 05:11

    It works this way (It looks like it doesn't work with POP3, but it works with IMAP):

         Properties props = new Properties();
         props.put("mail.store.protocol", "imaps");
         Session session = Session.getDefaultInstance(props, null);
         Store store = session.getStore("imaps");
         store.connect("imap.gmail.com", [theMailAccount@gmail.com], [thePasswordOrAppPassword]);
    
         // You possibly will have to use [Google Mail]/All Mail instead
         Folder inbox = store.getFolder("[Gmail]/All Mail");
    
    0 讨论(0)
  • 2020-12-13 05:25

    You can try the following code:

    private List<String> getResult1(){
      try {
        Properties props = new Properties();
        props.put("mail.store.protocol","imaps");
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect("imap.gmail.com", "Email Id", "App Password");
    
        //if you want mail from specified folder, just change change folder name
        //Folder inbox = store.getFolder("[Gmail]/Drafts");
        Folder inbox = store.getFolder("inbox");
    
        inbox.open(Folder.READ_ONLY);
        int messageCount = inbox.getMessageCount();
        Log.e("getFolder ", "getResult1: " + store.getDefaultFolder().list("*"));
        javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
    
        for (javax.mail.Folder folder : folders) {
          if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0){
            Log.e("getFolder ", "getResult1: " + folder.getName() );
          }
        }
    
        Log.e("Mail Subject:", "Total Messages:-: " + messageCount );
        javax.mail.Message[] messages = inbox.getMessages();
    
        System.out.println("------------------------------");
        Log.e("Mail Subject:", "messages: " + messages.toString());
        for (int i = 0; i < messages.length; i++) {
          Log.e("Mail Subject:", "getResult1: " + messages[i].getSubject());
        }
        inbox.close(true);
        store.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
    }
    
    0 讨论(0)
提交回复
热议问题