title: How to access gmail account via javamail
body: <p>I try to access email account on a certain email server via "imap" through java mail. I did some research on this. And I find the following code which works for gmail.</p>
  
  <pre><code>import java.io.*;
  import javax.mail.*;
  import javax.mail.internet.*;
  
  public class DeleteMessageExample {
      public static void main (String args[]) throws Exception {
      String host = args[0];
      String username = args[1];
      String password = args[2];
  
      // Get session
      Session session = Session.getInstance(
        System.getProperties(), null);
  
      // Get the store
      Store store = session.getStore("imaps");
      store.connect(host, username, password);
  
      // Get folder
      Folder folder = store.getFolder("INBOX");
      folder.open(Folder.READ_WRITE);
  
      BufferedReader reader = new BufferedReader (
        new InputStreamReader(System.in));
  
      // Get directory
      Message message[] = folder.getMessages();
      for (int i=0, n=message.length; i&lt;n; i++) {
         System.out.println(i + ": " + message[i].getFrom()[0] 
           + "\t" + message[i].getSubject());
  
         System.out.println("Do you want to delete message? [YES to delete]");
         String line = reader.readLine();
         // Mark as deleted if appropriate
         if ("YES".equals(line)) {
           message[i].setFlag(Flags.Flag.DELETED, true);
         }
      }
  
      // Close connection 
      folder.close(true);
      store.close();
          }
      }
  </code></pre>
  
  <p>However, I need to specify the args[0] to be imap.gmail.com  args[1] to be usrname, args[2] to be password. And if I replace imap.gmail.com by the ip address  74.125.224.86, it no longer work.</p>
  
  <p>My question is suppose I have an account on yahoo mail, what the hostname I should use?
  I tried imap.yahoo.com, mail.yahoo.com and the ip address. </p>
  
  <p>If you know the answer, would you mind also told me what is the regular rule to find out what kind hostname I should use?</p>
  
  <p>Thanks a lot.</p>