Bulk Fetch Mail Bodies Using Javamail API and IMAP - java

Is there a way to fetch the mail bodies of multiple emails with a single call to an IMAP server using the Javamail API?
I know I can get to the body of a given message using the Message.getContent() call, but this ends up doing a call to the imap server for each individual message.
Is it possible to use the FetchProfile and Folder.fetch call to bulk fetch bodies? The documentation implies that the FetchProfile is only for header data. I tried the following, but that didn't do the trick:
FetchProfile fp = new FetchProfile();
fp.add("rfc822.text");
inbox.fetch(messages, fp);
If it is not possible to do this using Javamail, is it due to a constraint in the Javamail API or does the IMAP protocol simply not support this?

Limitation of JavaMail. The IMAP protocol allows fetching the bodies of several messages at once:
a1 fetch 1:* (rfc822.header rfc822.text)

Related

How to differentiate between the automatic reply and the normal mail received

I am working on receiving mails in my springboot application. In order to fetch and store the receive mails. I am using imap mail listener. There are two types of mails which I am storing. One is multipart payload type and the other is string payload type.
After receiving mail I am trying to send an auto-generated mails using java mail.
The issue which I am facing is worst case scenario of generating auto-reply from my application i.e infinite loop.
Can someone help ow can I differentiate between a normal mail received and auto-reply received in my mail box and generate auto-replies from my system only for those mails which are not auto-reply type.
It would be nice if explained via code for headers check. I came across through few headers like x-Autosubmitted. But they are returning null if I am trying to print the values in console.
The auto-submmitted markers are described below that you may find helpful:
auto-generated - Indicates that a message was generated by an automatic process, and is not a direct response to another message.
auto-replied - Indicates that a message was automatically generated as a direct response to another message.
auto-notified - Indicates that a message was generated by a Sieve notification system.
no - Indicates that a message was NOT automatically generated, but was created by a human. It is the equivalent to the absence of an Auto-Submitted header altogether.
The RFC 2822 states the following:
Though optional, every message SHOULD have a "Message-ID:" field.
Furthermore, reply messages SHOULD have "In-Reply-To:"
So, you may check for the "In-Reply-To:" value in the header.
Also you may add your own value to the outgoing email, so you may distinguish between an automatically generated reply from your system and manually created.

Retrive a mail from gmail inbox for a particular subject

I have a scenario where I have to read a mail sent out by a X person with a specific subject, which i will be receiving on a daily basis.
Is there any JAVA Gmail APi provided by google to retrieve the recent mail that i have received.
And also is there a way to retrieve the mail for a given date?
Yes there's a Gmail API and it has a Java client library, you can check the Quickstart to get used to it.
Now, for retrieving a list of mails you will need to use the Users.messages: list endpoint ( There's also a Java example on how to use that endpoint). Answering your question about retrieving certain emails, you will need to use the q parameter and set the values there as if you would be searching an email in the gmail search bar. I will leave you an example that you can try using the Try this API:
List emails from certain user and date.
Notice
You will only get the email's IDs, for getting more info about an email, you will have to use the Users.messages: get endpoint.

Java SMTP accept badly formatted emails

I am using SubethaSMTP to proxy smtp emails. I have an application sending emails to this proxy.
It happens that sometimes the application send malformed emails (for historical reasons).
This I cannot change. This is a buggy external solutions.
I am handling the received emails and route them based on the email value.
My issue is when I call the following method on the received emails:
Address[] tos = message.getRecipients(RecipientType.TO);
When there are malformed emails in the recipients list, I cannot get the list because I receive an exception.
My objective is to get all the emails and correct the malformed ones.
But because of the exception, I cannot get them.
Is there a way to get all the recipient emails even if there are some bad ones? Just read them without any issue? Is it possible to bypass the controls?
The stack trace is: The email address is #TEST(value) . this is generated by the external application. I'd like to be able to get it, and remove it from the list, and correct it to resend it
javax.mail.internet.AddressException: Missing local name in string ``#TEST''
at javax.mail.internet.InternetAddress.checkAddress(InternetAddress.java:1216)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:1096)
at javax.mail.internet.InternetAddress.parseHeader(InternetAddress.java:663)
at javax.mail.internet.MimeMessage.getAddressHeader(MimeMessage.java:733)
at javax.mail.internet.MimeMessage.getRecipients(MimeMessage.java:565)
at MessageHandler.done(TestMessageHandler.java:128)
at org.subethamail.smtp.server.Session.endMessageHandler(Session.java:513)
at org.subethamail.smtp.server.Session.resetMessageState(Session.java:490)
at org.subethamail.smtp.command.DataCommand.execute(DataCommand.java:84)
at org.subethamail.smtp.server.RequireTLSCommandWrapper.execute(RequireTLSCommandWrapper.java:30)
at org.subethamail.smtp.server.CommandHandler.handleCommand(CommandHandler.java:99)
at org.subethamail.smtp.server.Session.runCommandLoop(Session.java:244)
at org.subethamail.smtp.server.Session.run(Session.java:145)

Prefetch preview text from JavaMail Message

I'm using JavaMail 1.5.2 to read messages from IMAP accounts. To reduce the number of requests to the host I prefetch some message data, like From, Date, Message-ID etc.:
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
fp.add("Message-ID");
Message msgs[] = folder.getMessages();
folder.fetch(msgs,fp);
However, I want to also prefetch some parts of the content to create a preview text for the mail without having to load the full message with all attachments. For example, I would like to prefetch all parts of the content that have the type "text/plain" and are no attachments. Is that possible?
PS: I'm not searching for a solution like fp.add(IMAPFolder.FetchProfileItem.MESSAGE) because this will prefetch the whole message with all attachments.
You have to retrieve the bodystructure first, then loop across the message structure, check the mime type of each part, and download the parts you want. IMAP lets you download all of the parts using one command, so if Javamail is a little smart, you should be able to do this with two IMAP commands, no matter how many bodyparts you end up wanting to download.
The IMAP commands, if you're the type who likes to look at wire traffic, should be something like a uid fetch 234789 bodystructure followed by b uid fetch 234789 (body.peek[1.1] body.peek[2]).

java mail - getting message by message ID

I'm developing Android mail client. I need to build a "conversation" structure for every email message. I use the
IMAPMessage.getInReplyTo()
method that returns the Message ID of message which the message is a reply to. Unfortunatelly there seems to be no easy way to obtain message from
IMAPFolder
using its message ID. It is only possible to get the message by its UID. Is there an easy way to get the IMAP message by its Message ID?
You can use the IMAPFolder's search method like this:
SearchTerm searchTerm = new MessageIDTerm(messageId);
Message[] messages = imapFolder.search(searchTerm);
See the docs for the IMAPFolder's search method here:
https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/IMAPFolder.html#search-javax.mail.search.SearchTerm-
and for the MessageIDTerm class here:
https://javaee.github.io/javamail/docs/api/javax/mail/search/MessageIDTerm.html
unfortunately there is no straight forward solution... May be what you can try is to maintain an internal structure with body structures of all the mail ids and then perform a one on one Message-ID check and obtain the UID of mail. Anyways you would be doing it, in order to show the Maillist. Add a new logic to map the message-ids as well.

Categories

Resources