Overview
Google allow Gmail account users to access their account using any capable email client such as Microsoft Outlook, Apple Mail.app and others. For this to work Gmail provides POP and IMAP access to these client applications so that they could access emails stored on Gmail servers. To retrieve mails however Gmail require that SSL be used when accessing their POP server. This enables encryption to secure mail during transmission.
The purpose of this article is to demonstrate how to securely connect to the Gmail server and retrieve mails using a bit of Java code that make use of com.jscape.inet.smtpssl.PopSsl class found in Secure iNet Factory.
The procedures in this article may also apply to other POP servers that provide secure SSL/TLS access.
Prerequisites
- A Gmail account
- Secure iNet Factory
- Java 1.4 or greater
Using PopSsl to Retrieve Email Securely from Gmail
The PopSsl class provides an easy to use client interface for securely communicating with POP3 servers using SSL/TLS. The PopSsl class provides both implicit and explicit SSL/TLS connections. Implicit SSL/TLS connections are made by default using remote port 995. Explicit SSL/TLS connections are made using an unencrypted connection to port 110 of a POP3 server and then switching to an encrypted mode using the STLS command prior to sending any additional data.
An implicit SSL/TLS connection on port 995 is what’s required to connect to Gmail POP server. Below is a complete Java code that uses PopSsl class to retrieve mails from Gmail.
//import the necessary classes
import com.jscape.inet.email.EmailMessage;
import com.jscape.inet.popssl.PopSsl;
public class PopSslGmail {
public static void main(String[] args) {
String hostname = "pop.gmail.com";
String username = "username@gmail.com"; //CHANGE THIS
String password = "password"; //CHANGE THIS
try {
// create new PopSsl instance
PopSsl ssl = new PopSsl(hostname, 995, username, password);
ssl.setDebug(true);
// establish SSL/TLS connection
ssl.connect();
// get message count
int count = ssl.getMessageCount();
// get each message and print subject
for(int i = 1; i <= count; ++i) {
EmailMessage msg = ssl.getMessage(i);
System.out.println("Subject: " + msg.getSubject());
}
// disconnect
ssl.disconnect();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Download the source code file here: PopSslGmail.java
Summary
This article demonstrates how straightforward and easy it is to connect to a secure POP server, in this case Gmail, and then retrieve emails.
References
PopSsl Overview
PopSsl Javadoc
Gmail - Configuring other mail clients
Comments