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
Prerequisites
- Account and POP server details
- JDK 1.4 or above
- Secure iNet Factory
Retrieving Email Using the Pop Class
The Pop class provides an easy to use programming interface for communicating with POP3 (Post Office Protocol version 3) servers.
Below is a sample code the illustrates how to use the Pop class to connect to a POP3 server then iterate and retrieve all available email messages. It demonstrate saving the messages as well as its attachments if available.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Enumeration;
import com.jscape.inet.email.EmailMessage;
import com.jscape.inet.mime.Attachment;
import com.jscape.inet.mime.MimeException;
import com.jscape.inet.pop.Pop;
import com.jscape.inet.pop.PopException;
public class PopAttachmentsTutorial {
private static File messagesDir = new File("msg");
private static File attachmentsDir = new File("attach");
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// get messages from Pop mailbox
public void getMessages(String hostname, String username, String password)
throws PopException, MimeException, IOException {
// ensure that messages directory is valid
if(!messagesDir.exists()) {
messagesDir.mkdir();
}
// ensure that attachments directory is valid
if(!attachmentsDir.exists()) {
attachmentsDir.mkdir();
}
// connect and login
Pop pop = new Pop(hostname,username,password);
pop.connect();
System.out.println("Retrieving messsages...");
// get message count
int messageCount = pop.getMessageCount();
if(messageCount < 1) {
System.out.println("No messages found");
}
// get messages and write messages to disk
for(int i = 1; i <= messageCount; ++i) {
EmailMessage msg = (EmailMessage)pop.getMessage(i);
File f = new File(messagesDir,"msg" + i + ".txt");
FileOutputStream fout = new FileOutputStream(f);
fout.write(msg.getMessage());
fout.close();
System.out.println("Retrieved message: " + f.getAbsoluteFile());
// write attachments to disk
int attachnum = 0;
Enumeration attachments = msg.getAttachments();
while(attachments.hasMoreElements()) {
++attachnum;
Attachment attach = (Attachment)attachments.nextElement();
String attachmentFileName = attach.getFilename();
if(attachmentFileName == null) {
attachmentFileName = "attachment" + i + "-" + attachnum;
}
String fileName = attach.getFilename();
if(fileName == null) {
fileName = System.currentTimeMillis() + ".txt";
}
File attFile = new File(attachmentsDir,fileName);
FileOutputStream attOut = new FileOutputStream(attFile);
try {
attOut.write(attach.getFileData());
attOut.close();
System.out.println("Retrieved attachment: " + attFile.getAbsoluteFile());
}
catch(Exception e) {
throw new PopException("unable to decode file attachment");
}
}
}
pop.disconnect();
}
public static void main(String[] args) {
String hostname;
String username;
String password;
try {
System.out.print("Enter Pop3 hostname (e.g. mail.domain.com): ");
hostname = reader.readLine();
System.out.print("Enter Pop3 Username: ");
username = reader.readLine();
System.out.print("Enter Pop3 Password: ");
password = reader.readLine();
PopAttachmentsTutorial tutorial = new PopAttachmentsTutorial();
tutorial.getMessages(hostname,username,password);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Download the source code here: PopAttachmentsTutorial.java
Summary
Using the Secure iNet Factory components retrieving email from a POP3 or IMAP server is a very simple task.
This article focus on the POP3 protocol but 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.
References
Comments