Get Email Message Example : Email « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Network Protocol » EmailScreenshots 
Get Email Message Example
 
// Fetching Mail

import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class GetMessageExample {
  public static void main(String args[]) throws Exception {
    if (args.length != 3) {
      System.err.println("Usage: java MailExample host username password");
      System.exit(-1);
    }

    String host = args[0];
    String username = args[1];
    String password = args[2];

    // Create empty properties
    Properties props = new Properties();

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    BufferedReader reader = new BufferedReader(new InputStreamReader(
        System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i = 0, n = message.length; i < n; i++) {
      System.out.println(i + ": " + message[i].getFrom()[0"\t"
          + message[i].getSubject());

      System.out.println("Read message? [YES to read/QUIT to end]");
      String line = reader.readLine();
      if ("YES".equalsIgnoreCase(line)) {
        System.out.println(message[i].getContent());
      else if ("QUIT".equalsIgnoreCase(line)) {
        break;
      }
    }

    // Close connection
    folder.close(false);
    store.close();
  }
}

           
         
  
Related examples in the same category
1.Pure Java Email client
2.Sending Mail Using Sockets
3.Sending Mail
4.A Client to Send SMTP MailA Client to Send SMTP Mail
5.Mailer: Sends an email message
6.TestOpenMailRelay -- send self-returning SPAM to check for relay sitesTestOpenMailRelay -- send self-returning SPAM to check for relay sites
7.Sender -- send an email message
8.Sender -- send an email message with attachment
9.SendMime -- send a multi-part MIME email message
10.Read a file return mail headers one at a time
11.Send mail using GMAIL
12.Send Mail Implementation using simple SMTP
13.Validator for Zip code, Email, Phone number
14.Send email out
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.