In this example you will learn how to receive email using POP3. We are going to connect to Gmail server and read the messages in the INBOX folder. There are some steps that you need to do to download this email. Here are the steps:
- Setup properties for the mail session.
- Creates a
javax.mail.Authenticator
object. - Creating mail session.
- Get the POP3 store provider and connect to the store.
- Get folder and open the INBOX folder in the store.
- Retrieve the messages from the folder.
- Close folder and close store.
These steps can be written as the following code snippet:
package org.kodejava.mail;
import javax.mail.*;
import java.util.Properties;
public class ReadEmail {
public static final String USERNAME = "kodejava";
public static final String PASSWORD = "password";
public static void main(String[] args) throws Exception {
// 1. Setup properties for the mail session.
Properties props = new Properties();
props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.socketFactory.fallback", "false");
props.put("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
props.put("mail.pop3.user", ReadEmail.USERNAME);
props.put("mail.store.protocol", "pop3");
props.put("mail.pop3.ssl.protocols", "TLSv1.2");
// 2. Creates a javax.mail.Authenticator object.
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(ReadEmail.USERNAME, ReadEmail.PASSWORD);
}
};
// 3. Creating mail session.
Session session = Session.getDefaultInstance(props, auth);
// 4. Get the POP3 store provider and connect to the store.
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", ReadEmail.USERNAME, ReadEmail.PASSWORD);
// 5. Get folder and open the INBOX folder in the store.
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
// 6. Retrieve the messages from the folder.
Message[] messages = inbox.getMessages();
for (Message message : messages) {
message.writeTo(System.out);
}
// 7. Close folder and close store.
inbox.close(false);
store.close();
}
}
Maven Dependencies
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Great tutorial! Thanks for the code!
Great tutorial! I was trying to access other folders like Drafts, Spam etc. but don’t seem to be succeeding. Any pointers on what should be done?
Thanks in advance!
Does the message get removed from the mail server, once downloaded to the local computer?
Since this is POP3, which (usually?) deletes the emails from the server, after retrieving them?