I am connecting to the a Gmail account and I try to read the unseen email using Jakarta Mail API. I try to use the tech.blueglacier.email-mime-parser library which is based on org.apache.james.mime4j in order to parse the Message and get the content of the mail. See the code from bellow.
public void fetchMail() throws MessagingException {
Folder folder = null;
try (Store store = Session.getDefaultInstance(imapMailConfig.imapMailProperties()).getStore("imap")) {
store.connect(imapMailConfig.imapMailProperties().getProperty("host"), username, password);
folder = store.getFolder(mailFolder);
folder.open(Folder.READ_ONLY);
log.info("Folder opened");
Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
log.info("Messages count: {}", messages.length);
ContentHandler contentHandler = new CustomContentHandler();
MimeConfig mime4jParserConfig = MimeConfig.DEFAULT;
BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();
MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig, DecodeMonitor.SILENT,bodyDescriptorBuilder);
mime4jParser.setContentDecoding(true);
mime4jParser.setContentHandler(contentHandler);
for (Message message : messages) {
// retrieve the content of the message as an InputStream
InputStream inputStream = message.getInputStream();
mime4jParser.parse(inputStream);
Email email = ((CustomContentHandler) contentHandler).getEmail();
List<Attachment> attachments = email.getAttachments();
Attachment calendar = email.getCalendarBody();
Attachment htmlBody = email.getHTMLEmailBody();
Attachment plainText = email.getPlainTextEmailBody();
String to = email.getToEmailHeaderValue();
String cc = email.getCCEmailHeaderValue();
String from = email.getFromEmailHeaderValue();
log.info("from: {}", from);
log.info("getEmailSubject: {}", email.getEmailSubject());
}
// close the store and folder objects
store.close();
log.info("Close store");
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
if (folder != null && folder.isOpen()) {
log.info("Close folder");
folder.close(true);
}
}
}
is returning the following error:
Folder opened
Messages count: 2
tech.blueglacier.email.Email : mime part received
tech.blueglacier.email.Email : Email plain text body identified
from: null
getEmailSubject: null
tech.blueglacier.email.Email : mime part received
null
java.util.EmptyStackException: null
at java.base/java.util.Stack.peek(Stack.java:101) \~\[na:na\]
at tech.blueglacier.email.Email.addPlainTextEmailBody(Email.java:202) \~\[email-mime-parser-1.0.5.jar:na\]
at tech.blueglacier.email.Email.fillEmailContents(Email.java:71) \~\[email-mime-parser-1.0.5.jar:na\]
at tech.blueglacier.parser.CustomContentHandler.body(CustomContentHandler.java:36) \~\[email-mime-parser-1.0.5.jar:na\]
at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:133) \~\[apache-mime4j-core-0.8.9.jar:0.8.9\]
at dev.fetchmail.koala.services.ImapMailReceiverImpl.fetchMail(ImapMailReceiverImpl.java:81) \~\[classes/:na\]
at dev.fetchmail.koala.KoalaApplication.run(KoalaApplication.java:21) \~\[classes/:na\]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:769) \~\[spring-boot-3.0.4.jar:3.0.4\]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:753) \~\[spring-boot-3.0.4.jar:3.0.4\]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) \~\[spring-boot-3.0.4.jar:3.0.4\]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) \~\[spring-boot-3.0.4.jar:3.0.4\]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) \~\[spring-boot-3.0.4.jar:3.0.4\]