Mój problem wygląda następująco:Pobieranie wiadomości e-mail zawierającej obrazy w wierszu w java
Mam konfigurację Mój kod do czytania wiadomości e-mail z określonego konta. Ta część działa idealnie.
Problem dotyczy parsowania wiadomości e-mail. Rozdzielanie załączników i treści wiadomości e-mail (zawierających obrazy wstawiane).
Mój kod wygląda tak:
Void readMessages(Folder folder){
Message[] messages = folder.getMessages();
// loading of message objects.
for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {
final Message currentMessage = messages[messageNumber];
logger.info("Handling the mail with subject " + currentMessage.getSubject());
logger.info("Content type for the current message is " + currentMessage.getContentType());
final String messageFileName = currentMessage.getFileName();
logger.info("File name for the message " + messageFileName + ". File name is blank "
+ StringUtils.isBlank(messageFileName));
Object messageContentObject = currentMessage.getContent();
if (messageContentObject instanceof Multipart) {
Multipart multipart = (Multipart) messageContentObject;
// downloading all attachments....
int attachmentCount = multipart.getCount();
logger.info("Number of attachments ");
for (int i = 0; i < attachmentCount; i++) {
Part part = (Part) multipart.getBodyPart(i);
downloadAttachment(part, folderPath.toString());
}
}
}
}
}
private void downloadAttachment(Part part, String folderPath) throws Exception {
String disPosition = part.getDisposition();
String fileName = part.getFileName();
String decodedText = null;
logger.info("Disposition type :: " + disPosition);
logger.info("Attached File Name :: " + fileName);
if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {
logger.info("DisPosition is ATTACHMENT type.");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName != null && disPosition == null) {
logger.info("DisPosition is Null type but file name is valid. Possibly inline attchment");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName == null && disPosition == null) {
logger.info("DisPosition is Null type but file name is null. It is email body.");
File file = new File(folderPath + File.separator + "mail.html");
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
}
}
protected int saveEmailAttachment(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = null;
InputStream is = null;
int ret = 0, count = 0;
try {
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
part.writeTo(new FileOutputStream(saveFile));
} finally {
try {
if (bos != null) {
bos.close();
}
if (is != null) {
is.close();
}
} catch (IOException ioe) {
logger.error("Error while closing the stream.", ioe);
}
}
return count;
}
Problem I get to kiedy uruchomić ten kod, otrzymuję plik HTML ale obrazy inline otrzymuje znak błędu obrazu, który wskazuje obraz z brak źródła.
Proszę mi pomóc. Daj mi znać, jeśli potrzebujesz więcej informacji.
Próbowałem też oszczędność ciało jako plik .eml
zmieniając:
File file = new File(folderPath + File.separator + "mail.html");
do
File file = new File(folderPath + File.separator + "mail.eml");
ale mam takie same wyniki.
"..przez znak dla obrazu błędu, który wskazuje obraz bez źródła." Czy na pewno źródło jest puste? Czy sprawdziłeś źródło HTML? – Janoz
Sprawdź ten plik HTML. https://www.dropbox.com/s/5evk4pjq721yo8m/mail.html – GSG
Ten kod HTML pokazuje brakujące źródło obrazu. – GSG