Retrieve attachment

I'm using this code to retrieve email with a simple txt file as attachment:

Properties props = new Properties();

Session session = Session.getDefaultInstance(props, null);

Store store = session.getStore("pop3");

store.connect(host, username, password);

Folder folder = store.getFolder("INBOX");

folder.open(Folder.READ_ONLY);

Message message = folder.getMessage(1);

Multipart multipart = (Multipart) message.getContent();

// Process each part of the message

for (int i=0; i<multipart.getCount(); i++) {

processPart(multipart.getBodyPart(i));

}

folder.close(false);

store.close();

}

catch (MessagingException me) {

System.err.println(me.getMessage());

}

catch (IOException ioe) {

System.err.println(ioe.getMessage());

}

catch(ClassCastException e)

{

System.err.println(e.getMessage());

}

}

private static void processPart(Part part)

throws MessagingException, IOException{

String disposition = part.getDisposition();

String fileName = part.getFileName();

if (disposition.equals(Part.ATTACHMENT)) {

// Its an attachment

if (fileName == null) {

// the file name is null, so assign a name

fileName = File.createTempFile("attachment", ".txt").getName();

}

// write the part to a file

writeFile(fileName, part.getInputStream());

}

else {

// the disposition is either INLINE or null

}

}

private static void writeFile

(String fileName, InputStream in) throws IOException {

// Do no overwrite existing file

File file = new File(fileName);

for (int i=0; file.exists(); i++) {

file = new File(fileName+i);

}

// Write the part to file

BufferedOutputStream bos =

new BufferedOutputStream(new FileOutputStream(file));

BufferedInputStream bis = new BufferedInputStream(in);

int aByte;

while ((aByte = bis.read()) != -1) {

bos.write(aByte);

}

bos.flush();

bos.close();

bis.close();

}

I'm getting the same output all over again :"java.lang.String cannot be cast to javax.mail.Multipart".What am I doing wrong?>

[2283 byte] By [Shmeka] at [2007-11-27 11:18:05]
# 1

what line of code does your error reference?

petes1234a at 2007-7-29 14:28:45 > top of Java-index,Java Essentials,New To Java...
# 2

It refers to this line : Multipart multipart = (Multipart) message.getContent();

Shmeka at 2007-7-29 14:28:45 > top of Java-index,Java Essentials,New To Java...
# 3

it appears that the contentType of your message retrieved is a string, not a Multipart. Can you check this before casting by calling a message.getContent() method?

petes1234a at 2007-7-29 14:28:45 > top of Java-index,Java Essentials,New To Java...
# 4

I used getContentType() method.Content type appears to be text/plain.The original email on my server contains some text and an attachment which is a small .txt file.

Now that I know content type how can I retrieve that email?

Shmeka at 2007-7-29 14:28:45 > top of Java-index,Java Essentials,New To Java...
# 5

I'm no expert in java email, but, well if it's text that you're dealing with, can't you just put it into a String rather than trying to squeeze it (unsuccessfully) into a Multipart?

petes1234a at 2007-7-29 14:28:45 > top of Java-index,Java Essentials,New To Java...
# 6

You should be doing something like this

Object content = message.getContent();

if (content instanceof Multipart) {

dealWithMulti( (MultiPart) content)

} else {

dealWithPart(message)

}

public void dealWithMulti(MultiPart parts) {

for part in parts dealWithSingle(part)

}

public void dealWithSingle(Part part) {

//process part

}

_helloWorld_a at 2007-7-29 14:28:45 > top of Java-index,Java Essentials,New To Java...