Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

Retrieving email from POP3 server using Java

Overview

This article will demonstrate how to retrieve email from a POP3 server using the components provided in Secure iNet Factory. To see what else Secure iNet Factory has to offer Download a FREE 30 day Secure iNet Factory Evaluation.

Retrieving email

Using the Secure iNet Factory components retrieving email from a POP3 or IMAP server is a very simple task. For the purposes of this article we will focus on using the POP3 protocol. Details of using the IMAP protocol to retrieve email are very much the same, the main difference being that the com.jscape.inet.pop.Pop class is used instead of the com.jscape.inet.imap.Imap class.

Example

001 /*
002  * @(#)PopAttachmentsExample.java
003  *
004  * Copyright (c) 2001-2002 JScape
005  * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
006  * All rights reserved.
007  *
008  * This software is the confidential and proprietary information of
009  * JScape. ("Confidential Information").  You shall not disclose such
010  * Confidential Information and shall use it only in accordance with
011  * the terms of the license agreement you entered into with JScape.
012  */
013 
014 import com.jscape.inet.pop.*;
015 import com.jscape.inet.mime.*;
016 import com.jscape.inet.email.*;
017 import java.util.Enumeration;
018 import java.io.*;
019 
020 public class PopAttachmentsExample {
021     private static File messagesDir = new File("msg");
022     private static File attachmentsDir = new File("attach");
023     static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
024     
025     // get messages from Pop mailbox
026     public void getMessages(String hostname, String username, String password)
027     throws PopException, MimeException, IOException {
028         // ensure that messages directory is valid
029         if(!messagesDir.exists()) {
030             messagesDir.mkdir();
031         }
032         
033         // ensure that attachments directory is valid
034         if(!attachmentsDir.exists()) {
035             attachmentsDir.mkdir();
036         }
037         
038         // connect and login
039         Pop pop = new Pop(hostname,username,password);
040         pop.connect();        
041     
042     System.out.println("Retrieving messsages...");        
043         
044         // get messages
045         Enumeration messages = pop.getMessages();
046         if(!messages.hasMoreElements()) {
047             System.out.println("No messages found");
048         
049         
050         // write messages to disk
051         int msgnum = 0;
052         while(messages.hasMoreElements()) {
053             ++msgnum;
054             EmailMessage msg = (EmailMessage)messages.nextElement();
055             File f = new File(messagesDir,"msg" + msgnum + ".txt");
056             FileOutputStream fout = new FileOutputStream(f);
057             fout.write(msg.getMessage());
058             fout.close();
059             System.out.println("Retrieved message: " + f.getAbsoluteFile());
060             
061             // write attachments to disk
062             int attachnum = 0;
063             Enumeration attachments = msg.getAttachments();
064             while(attachments.hasMoreElements()) {
065                 ++attachnum;
066                 Attachment attach = (Attachment)attachments.nextElement();
067                 String attachmentFileName = attach.getFilename();
068                 if(attachmentFileName == null) {
069                     attachmentFileName = "attachment" + msgnum + "-" + attachnum;
070                 }
071                 
072                 String fileName = attach.getFilename();
073                 if(fileName == null) {
074                   fileName = System.currentTimeMillis() ".txt";
075                 }
076                 File attFile = new File(attachmentsDir,fileName);
077                 FileOutputStream attOut = new FileOutputStream(attFile);
078                 try {
079                     attOut.write(attach.getFileData());
080                     attOut.close();
081                     System.out.println("Retrieved attachment: " + attFile.getAbsoluteFile());
082                 }
083                 catch(Exception e) {
084                     throw new PopException("unable to decode file attachment");
085                 }
086             }
087         }
088         pop.disconnect();
089     }
090     
091     public static void main(String[] args) {
092         String hostname;
093         String username;
094         String password;
095         
096         try {
097             System.out.print("Enter Pop3 hostname (e.g. mail.domain.com): ");
098             hostname = reader.readLine();
099             System.out.print("Enter Pop3 Username: ");
100             username = reader.readLine();
101             System.out.print("Enter Pop3 Password: ");
102             password = reader.readLine();
103             PopAttachmentsExample example = new PopAttachmentsExample();
104             example.getMessages(hostname,username,password);
105         }
106         catch(Exception e) {
107             e.printStackTrace();
108         }
109     }
110 }

 

  1. Lines 14-18. Add necessary import statements.
  2. Line 26. The method for retrieving email messages from POP3 server.
  3. Lines 29-31. Create a directory named "msg" for storing raw message contents.
  4. Lines 34-36. Create a directory named "attach" for storing attachments retrieved from raw message contents.
  5. Lines 39-40. Create a new Pop instance and establish connection to POP3 server.
  6. Lines 45-48. Retrieve all messages from POP3 server
  7. Line 52. Loop through all messages retrieved.
  8. Lines 54-58. Write raw contents of message to "MSG" directory.
  9. Lines 63-86. Get attachments from message and write attachments to "attach" directory
  10. Line 88. Disconnect from POP3 server.
  11. Lines 91-109. The main() method for running this example.

Summary

In this article you learned how to retrieve email messages from a POP3 server. The POP3 and IMAP components in Secure iNet Factory make this easy removing the complexities of the POP3 and IMAP protocols. To see what else Secure iNet Factory has to offer Download a FREE 30 day iNet Factory Evaluation.