Retrieving email from POP3 server using Java
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.
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.
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 }
- Lines 14-18. Add necessary import statements.
- Line 26. The method for retrieving email messages from POP3
server.
- Lines 29-31. Create a directory named "msg" for
storing raw message contents.
- Lines 34-36. Create a directory named "attach"
for storing attachments retrieved from raw message contents.
- Lines 39-40. Create a new Pop instance and establish connection
to POP3 server.
- Lines 45-48. Retrieve all messages from POP3 server
- Line 52. Loop through all messages retrieved.
- Lines 54-58. Write raw contents of message to "MSG"
directory.
- Lines 63-86. Get attachments from message and write attachments
to "attach" directory
- Line 88. Disconnect from POP3 server.
- Lines 91-109. The main() method for running this example.
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.
|