The following code checks and reads emails and determines if any have
attachments.
I cannot get the correct filename from the attachment. They all come back as
winmail.dat and when I save them they have header and footer information.
Is this still MIME encoded and how can I get the encoded filename and data
stream?
Thanks
Stuart
public class GetMessageExample {
public static void main (String args[]) throws Exception {
String host = args[0];
String username = args[1];
String password = args[2];
// Create empty properties
Properties props = new Properties();
// Get session
Session session = Session.getDefaultInstance(props, null);
// Get the store
Store store = session.getStore("pop3");
// Connect to store
store.connect(host, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
// Open read-only
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
// Get directory
for (int i=0, n=message.length; i<n; i++) {
// Display from field and subject
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
System.out.println("Do you want to read message? [YES to read/QUIT to
end]");
String line = reader.readLine();
if ("YES".equals(line)) {
// Display message content
// System.out.println(message[i].getContent());
System.out.println(message[i].getContentType());
MimeMultipart multipart = (MimeMultipart) message[i].getContent();
System.out.println(multipart.getCount());
for (int p=0, q=multipart.getCount(); p<q; p++) {
Part part = multipart.getBodyPart(p);
System.out.println(part.getContentType());
DataHandler dh = part.getDataHandler();
System.out.println(dh.getName());
String disposition = part.getDisposition();
if ((disposition != null) &&
((disposition.equals(Part.ATTACHMENT) ||
(disposition.equals(Part.INLINE))))) {
saveFile(part.getFileName(), part.getInputStream());
}
}
} else if ("QUIT".equals(line)) {
break;
}
}
// Close connection
folder.close(false);
store.close();
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html