POP3 programs
• POP3
(Post Office Protocol 3) is the most recent version of a standard protocol for
receiving e-mail. POP3 is a client/server protocol in which e-mail is received
and held for you by your Internet server.
•
Periodically, client e-mail receiver checks your
mail-box on the server and downloads any mail, probably using POP3.
• This
standard protocol is built into most popular e-mail products, such as Eudora
and Outlook Express. It's also built into the Netscape and Microsoft Internet
Explorer browsers.
• POP3 is
designed to delete mail on the server as soon as the user has downloaded it.
However, some implementations allow users or an administrator to specify that
mail be saved for some period of time. POP can be thought of as a
"store-and-forward" service.
Steps to read a remote mailbox
• Set up
the properties use for the connection.
• Construct
the Authenticator use for the connection.
• Get a
Session object with Session.getDefaultInstance( ).
• Use the
session's getStore( ) method to return a Store.
• Connect
to the store.
• Get the
INBOX folder from the store with the getFolder( ) method.
• Open the
INBOX folder.
• Open the
folder inside the INBOX folder. Repeat as many times as necessary to reach the
folder you're seeking.
• Get the
messages from the folder as an array of Message objects.
• IterateMessageothervitalthroughclassinformation.Fortheinstance,arrayofGUImessages,printforoutthe
achprocessingusermessagtoselectachor.simplyoneindisplayturnusingthe
sender,themethodssubject,of andth
• Close the
folder.
• Close the
store.
Example
A program
that downloads and prints out the contents of a specified POP mailbox. Messages
are simply dumped on System.out in the default encoding. The servers,
usernames, and so forth are all hard coded.
import
javax.mail.*;
import
javax.mail.internet.*; import java.util.*;
import
java.io.*;
public
class POP3Client {
public static void main(String[] args) { Properties
props = new Properties( ); String host = "utopia.poly.edu"; String
username = "myuser";
String
password = "mypwd"; String provider = "pop3"; try {
//
Connect to the POP3 server
Session
session = Session.getDefaultInstance(props, null); Store store =
session.getStore(provider); store.connect(host, username, password);
// Open
the folder
Folder
inbox = store.getFolder("INBOX"); if (inbox == null) {
System.out.println("No
INBOX"); System.exit(1);
}
inbox.open(Folder.READ_ONLY);
// Get the messages from the server Message[]
messages = inbox.getMessages( ); for (int i = 0; i < messages.length; i++) {
System.out.println("-------
Message " + (i+1) + " ---------
");
messages[i].writeTo(System.out);
}
// Close the
connection
// but don't
remove the messages from the server inbox.close(false);
store.close(
);
}
catch
(Exception e) { e.printStackTrace();}
}
}
• The first
is to set up the properties for the mail session. Properties you might want to
set include mail.host, mail.store.protocol, mail.user, mail.pop3.user, and
mail.pop3.host. However, you don't absolutely need to set any of these. If the
Session will be used only to retrieve mail, then an empty Properties object
will be enough.
• To create
an instance of the javax.mail.Authenticator class (more properly, an instance
of a concrete subclass of the abstract Authenticator class) that can ask the
user for her password. For now, we'll simply hardcode those values and pass
null instead of an actual Authenticator.
• Use
Properties and Authenticator objects to get a Session instance.
• Now the
store is connected and ready to open a folder in the store.
• The
Message class provides many methods for working with individual messages. It
has methods to get the various header fields of the message, to get the content
of the message, to reply to the message, and more. Now print each message on
System.out using the message's writeTo( ) method:
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.