JSCAPE
  • Products
    • Managed File Transfer
    • JSCAPE SaaS
    • MFT Gateway
    • MFT Monitor
    • All Products
  • Solutions
    • All Solutions
    • Secure File Transfer
    • AS2 Server Software
    • File Synchronization
    • Reverse Proxy
    • Compliance
    • DMZ Streaming
  • Pricing
  • Company
    • Blog
    • Company
    • Contact Us
    • Clients
    • Case Studies
    • Testimonials
    • Certifications
  • Support
    • Help Desk
    • Documentation
    • Customer Downloads
Get a demo

Securely retrieving email from GMail using java POP library

Words by

Charles Ott

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…

Published in:

Blog

/

Secure File Transfer

java pop with java gmail

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.

Easy To Deploy, Easy To Administer, Easy To Manage

Ready to see how JSCAPE makes managed file transfer so much simpler? Schedule your demo now.

Request a demo

Popular Articles

View more by

JSCAPE
  •  
    1–2 minutes
    23/09/2025

    JSCAPE by Redwood, version 2025.3: New UI continues to make a splash

    Dive back into JSCAPE by Redwood with version 2025.3, bringing more modern UI updates to make using and navigating JSCAPE more intuitive. 

    Read article

  •  
    1–2 minutes
    19/08/2025

    Weathering the economic storm: Expert support is your enterprise file transfer system’s lifeline

    The global economic landscape can be described by one word: “uncertain”. Lingering effects from the 2020 pandemic, combined with new waves of international tariffs throughout 2025, have…

    Read article

  •  
    1–2 minutes
    30/07/2025

    Escape the grip: Why flexible MFT is key to enterprise agility

    Break free from costly vendor lock-in Let’s be blunt: some MFT vendors have built product suites that are less about true partnership and more about proprietary siloing.…

    Read article

  •  
    1–2 minutes
    23/07/2025

    Avoid downtime and threat actors: Getting the best of both worlds in MFT

    For enterprise organizations relying on managed file transfer (MFT) solutions, cybersecurity often feels like an arms race. The need to patch MFT software vulnerabilities to prevent breaches…

    Read article

Related Content

Read more about

JSCAPE MFT
  •  
    1–2 minutes
    30/07/2025

    Escape the grip: Why flexible MFT is key to enterprise agility

    Break free from costly vendor lock-in Let’s be blunt: some MFT vendors have built product suites that are less about true partnership and more about proprietary siloing.…

    Read article

  •  
    1–2 minutes
    03/07/2025

    Consolidate, deploy and thrive: JSCAPE’s formula for MFT success in uncertain times

    Global enterprises today are navigating a landscape marked by significant economic volatility. Fluctuating markets, shifting trade policies and persistent economic uncertainty are compelling enterprise organizations to reevaluate…

    Read article

  •  
    1–2 minutes
    31/03/2025

    SFTP vs. FTPS: Which file transfer software is best for business use?

    What is SFTP? SFTP is a file transfer protocol that’s normally packaged with Secure Shell (SSH), the network protocol most IT administrators use to access and manage…

    Read article

  •  
    1–2 minutes
    25/03/2025

    EDI integration with trading partners: 6 best practices

    Many large companies with high-volume transactions, especially those involved in e-commerce, manufacturing and retail supply chains, have long migrated from manual processes to Electronic Data Interchange (EDI).…

    Read article

Company

  • About Us
  • Contact Us
  • Clients
  • Case Studies
  • Testimonials
  • Privacy Policy
  • Certifications

Resources

  • Managed File Transfer
  • Secure File Transfer
  • Secure FTP Server
  • AS2 Server
  • Reverse Proxy
  • File Upload Processing
  • What Is An AS2 Server?

Support

  • Help Desk
  • Documentation
  • Customer Downloads
JSCAPE

Copyright © 2025 JSCAPE

  • Privacy Policy
  • Cookie Settings