Problem reading attachment of single part e-mail
hi, I have a problem with the javax.mail
If an e-mail have content and attachment , the program could treat it like a multipart e-mail, then I can parse it and read the content of attachment.
but there are some e-mails without text , only with attachment, I just found out the program treat it like single part e-mail, then I use getContent() method to get the content of the attachment, it gaves weird letters .
Can anyone help me with this?
Here is some part of my code
content = ((MimeMessage) messages).getContent().toString();
body = messages.getContent();
if ( body instanceof Multipart )
{
}
else
{ System.out.println(content);
}
If you're getting messages that are not multipart, and you want to treat
the single part of the message as an attachment, that's fairly easy,
but you need to decide how you're going to recognize that case and
distinguish it from a single part message with no "attachment".
You might try something like this:
if (msg.isMimeType("multipart/*")) {
// handle mutipart case
} else {
// single part case
String disp = msg.getDisposition();
if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT)) {
// handle message content as an attachment
} else {
// message with no attachment
}
}
Then what makes you believe it is intended to be an attachment?
Are you just guessing, or do you have some other knowledge
that tells you it definitely is an attachment?
Unless you know how to make that decision, it's going to be hard
to program your application to make that decision.
Well, again, how is it that you know it's an attachment?
Is it only because your friend told you? That's going to be
hard for your program to replicate! Or is it because the message
has a filename? Is it because the message is not a text MIME type?
Think about how it is you know and you can probably make your
program do the same thing.