Witam,
siedzę już nad tym tydzień, przeczytałem chyba wszystkie tutoriale, manuale itp. Mam problem z odebraniem wiadomości za pomocą biblioteki Javamail. Oto mój kod:
for (int i = 0; i < arrayMessages.length; i++) {
Message message = arrayMessages[i];
Address[] fromAddress = message.getFrom();
String from = MimeUtility.decodeText(fromAddress[0].toString().replaceAll("\"", ""));
String subject = message.getSubject();
String sentDate = message.getSentDate().toString();
String contentType = message.getContentType();
String messageContent = "";
// store attachment file name, separated by comma
String attachFiles = "";
int numberOfParts = 1;
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
BodyPart part = multiPart.getBodyPart(partCount);
String disposition = part.getDisposition();
InputStream inputStream = null;
if (disposition == null)
{
MimeBodyPart mbp = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (mbp.getContent() instanceof MimeMultipart){
MimeMultipart mmp = (MimeMultipart) mbp.getContent();
messageContent = mmp.getBodyPart(0).getContent().toString();
//System.out.println("bodyContent " + bodyContent);
}
else
{
messageContent = multiPart.getBodyPart(partCount).getContent().toString();
}
}
else if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
//part.saveFile(saveDirectory + File.separator + fileName);
}else if (Part.INLINE.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
// mbp.saveFile(saveDirectory + File.separator + fileName);
}
else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {messageContent = content.toString(); }
}
I teraz tak wiadomości typu text/plain, text/html pobiera dobrze. Problem jest z wiadomościami multipart/related, gdy wiadomość ma załączniki i w treści jest html, wtedy niektóre wiadomości pobiera a niektóre nie. Zauważyłem że jest to zależne od tej linijki:
messageContent = mmp.getBodyPart(0).getContent().toString();
Jeśli zamiast "0" dam "partCount" pobiera wszystkie oprócz jednej konkretnej, jeśli zamiast "0" dam "1" pobiera mi tą jedną konkretną a nie pobiera innych. numberOfParts tej jednej konkretnej wiadomości wynosi "3", a tych innych "2". Już nie mam pojęcia co jest nie tak, może źle są przekazywane parametry?