Receiving email in Java
For receiving email Store and Folder classes are used in collaboration with MimeMessage, Session and Transport classes.
For better understanding of this example, learn the steps of sending email using JavaMail API first.
For sending the email using JavaMail API, you need to load the two jar files:
- mail.jar
- activation.jar
download these jar files (or) go to the Oracle site to download the latest version.
Steps for receiving the email using JavaMail API
There are 5 steps to receive the email using JavaMail API. They are:
- Get the session object
- create the POP3 store object and connect with the pop server
- create the folder object and open it
- retrieve the messages from the folder in an array and print it
- close the store and folder objects
Example of Receiving email in Java
Let’s see the example of receiving the mail using java mail api.
As you can see in the above example, userid and password need to be authenticated. As, this program illustrates, you can receive email easily but change the username and password accordingly. Let’s see how to run it once again by simple technique:
Load the jar file | c:> set classpath=mail.jar;activation.jar;.; |
compile the source file | c:> javac ReceiveMail.java |
run by | c:> java Receivemail |