Securely Retrieving Email from GMail using Java POP Library
Using JSCAPE's Secure iNet Factory library, it's not hard to retrieve your email easily and with complete security from GMail, Hotmail or other servers that require APOP authentication, and we'll show you how in this article. On the other hand, how does your hand-addressed Christmas card get all the way across America to your Aunt Petunia's house, traveling over purple mountain majesties and across fruited plains, carried by trains and airplanes and, for Pete's sake, by a friendly mail person who walks it down the street? This process is a complete mystery and some scientist should investigate it!
But getting mail from GMail just requires using the PopSsl class from the library. This version of the POP3 protocol encrypts your mail request using SSL/TLS (Secure Sockets Layer / Transport Layer Security). This example extracts your mail from GMail and prints out various parts of each message:
Retrieving Email from GMail using Java
import com.jscape.inet.email.EmailMessage; import com.jscape.inet.popssl.PopSsl; public class SecurePOPMail { public static void main(String[] args) { // This hostname is for Gmail and is the same for everyone. Hotmail is "pop3.live.com", // and other services will be similar. String hostname = "pop.gmail.com"; String username = "rufus@gmail.com"; // These lines are your Gmail or Hotmail login String password = "throw1momma2from3the4train5a6kiss"; String msgSubject; // Various parts of an EmailMessage String msgFrom; String msgTo; String msgDate; String msgBody; // Other receivers of this message. "CC" is "carbon copy", if you didn't know, and // "BCC" is "blind carbon copy". When was the last time you saw carbon paper? String msgCC; String msgBCC; try { // Create new PopSsl instance and establish the connection to the POP3 server. "995" is // the port number and triggers "implicit" SSL/TLS encryption for the whole message. PopSsl ssl = new PopSsl(hostname, 995, username, password); ssl.setDebug(true); ssl.connect(); // Get the message count, then print selected parts of each message. int count = ssl.getMessageCount(); for(int i = 1; i <= count; ++i) { // Create an EmailMessage instance, then get all the parts separately. EmailMessage msg = ssl.getMessage(i); msgSubject = msg.getSubject(); msgFrom = msg.getFrom(); msgTo = msg.getTo(); msgDate = msg.getDate(); msgBody = msg.getBody(); msgCC = msg.getCc(); msgBCC = msg.getBcc(); // This is just to show that you can manipulate message parts. If the // Subject includes the word "work", don't print it, because // it's sure to be trouble for somebody! if (msgSubject.indexOf("work") <= 0) { System.out.println("Subject: " + msg.getSubject()); } else { System.out.println("Subject: [You don't want to know]"); } // Now print the message parts. System.out.println("------------------------"); System.out.println("From: " + msg.getFrom()); System.out.println("Date: " + msg.getDate()); System.out.println("CC: " + msg.getCc()); System.out.println("BCC: " + msg.getBcc()); System.out.println("Body: " + msg.getBody()); System.out.println("------------------------"); } // Disconnect ssl.disconnect(); } catch(Exception e) { e.printStackTrace(); } } }
The output of this program looks like this (with added comments):
+OK BLU0-POP558 POP3 server ready // Establish a session with a unique ID USER rufus@hotmail.com // Login and password +OK password required PASS ****** +OK mailbox has 2 messages // Various back-and-forth messages STAT +OK 4 131134 LIST 1 +OK 1 18550 RETR 1 +OK 18555 byte(s) Subject: Greetings Dear Friend! // First email ------------------------ From: <sincere@greatsincerity.com> Date: Sat, 17 Dec 2011 04:06:12 -0800 CC: null BCC: null Body: I have the honor to represent a Nigerian prince who expects to receive ... ------------------------ LIST 2 +OK 2 37055 RETR 2 +OK 37060 byte(s) Subject: Last-Minute Bargains // Second email ------------------------ From: TerrificDeals <buynow@terrificdeals.com< Date: Sat, 17 Dec 2011 09:37:05 -0500 CC: null BCC: null Body: Everybody wants an automatic coffee stirrer! ... ------------------------ QUIT +OK mailbox unchanged, POP3 server signing off // Disconnect
POP3 protocol with APOP authentication is a good way to fetch all your email from commercial servers so your program can examine and handle it.
Complete a short form for your free trial of JSCAPE MFT Server.