Search code examples
javaspring-bootemailimapmultipart

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.


Solution

  • 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.