Java Reference
In-Depth Information
After connecting to the Store, get a folder and open it. Using POP3, the only folder available is the
INBOX . Once the folder is open, you can read messages from it, as shown here:
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
The folder.getMessages() method uses lazy data retrieval . In other words, the message content is
only downloaded when specifically requested. You can get the content of a message with
getContent() or write the content to a stream with writeTo() . The getContent() method only
gets the message content, and writeTo() output includes headers. Here's an example:
System.out.println(((MimeMessage)message).getContent());
Notice that the INBOX is opened in READ_ONLY mode. Write access can be used to mark messages as
received or to delete them from the server. Listing 16-3 illustrates how easy it is to receive e-mail
messages using the JavaMail API.
Listing 16-3: Reading e-mail using JavaMail and saving it to a database
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
public class JavaMailReceiver{
static String server="mail.home.com";
static String username="user";
static String password="password";
static MailSaver db = new MailSaver();
public static void main(String args[]){
try {
receive(server, username, password);
}catch (Exception e){
System.err.println(e);
}
System.exit(0);
}
public static void receive(String server,
String username, String password){
Store store=null;
Folder folder=null;
Search WWH ::




Custom Search