Home Javamail Read Mail using James Server



Read Mail using James Server
Posted on: December 31, 2008 at 12:00 AM
This application illustrates how to read mail from mail box and how to select message from database table.

Read Mail using James Server

     

 This application illustrates how to read mail from mail box and how to select message from database table.

In this example we read the mail from mail box with send date, from, subject and message which showing in the figure below. We also retrieve the all messages from database table which also display in figure below.

We are using javax.mail api to read the messages from mail box.

The Complete Application is as follows:

 

Source Code of ReadMail.java 

import java.io.*;
import java.util.*;
import javax.mail.*;
import java.sql.*;
import java.text.*;

public class ReadMail {
  public static void main(String args[]) throws Exception {
  String host = "192.168.10.204";
  String user = "sandeep";
  String password = "sandeep";
  Properties properties = System.getProperties();
  Session session = Session.getDefaultInstance(properties);
  Store store = session.getStore("pop3");
  store.connect(host, user, password);
  Folder folder = store.getFolder("inbox");
  folder.open(Folder.READ_ONLY);

  Message[] message = folder.getMessages();

  for (int i = 0; i < message.length; i++) {
  System.out.println("------------ Message " + (i + 1) + " ------------");
  System.out.println("SentDate : " + message[i].getSentDate());
  System.out.println("From : " + message[i].getFrom()[0]);
  System.out.println("Subject : " + message[i].getSubject());
  System.out.print("Message : ");
  InputStream stream = message[i].getInputStream();
  while (stream.available() != 0) {
  System.out.print((char) stream.read());
  }
  }
  folder.close(true);
  store.close();

  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "test";
  String driver = "com.mysql.jdbc.Driver";
  String username = "root"; 
  String userPassword = "root";
  
  try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,username,userPassword);
  Statement st = conn.createStatement();
  ResultSet rs = st.executeQuery("select message from message");
  while (rs.next()){
  String msg = rs.getString("message");
  System.out.println(msg);
  }
  System.out.println("Query Executed Successfully...");  
  } catch (Exception e) {
  System.out.println(e.getMessage());
  } finally {
  conn.close();
  }
  }
} 

Download Source Code

Related Tags for Read Mail using James Server:
cdatabasetabledataselectapplicationiomailreadthismessageaiapptabboxtobaseeillifromasmtrsagecaadesaseagemehowpprateratescatsssaessatislleaandstrsassthstabablatiapicaicaplndonomo


More Tutorials from this section

Ask Questions?    Discuss: Read Mail using James Server  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.