Amith
Amith

Reputation: 1967

IMAP folder receives email Twice

I am trying to develop an email listener using Java mail api. I am receiving same email twice, the scenario not happening every time; randomly occurs.

The following code is used to make IMAP connection

try {
            isConnecting.set(true);
            createSession();
            LOGGER.warn("session created "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
            store = session.getStore("imap");
            if(store==null){
                LOGGER.warn("store is null return from here"+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                return;
            }
            store.connect(imapServerAddress, imapLoginId, imapPasswrd);
            LOGGER.warn("Connecting successful "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
            folder = store.getFolder(folderName);
            LOGGER.warn("Folder creating "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
            if (timer == null) {
                timer = new HashedWheelTimer(1, TimeUnit.SECONDS);
            }
            if (idleManager == null) {
                idleManager = new IdleManager(session, es);
            }
            if (folder == null || !folder.exists()) {
                LOGGER.warn("invalid folder exit from here "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                System.out.println("Invalid folder");
                return;
            }
            synchronized (folder) {
                LOGGER.warn("Folder sync "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                folder.addConnectionListener(this);
                folder.open(Folder.READ_WRITE);
                folder.addMessageCountListener(this);

                System.out.println("message count listener added");
                LOGGER.warn("message count listner added "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                if (folder instanceof IMAPFolder) {
                    IMAPFolder f = (IMAPFolder) folder;
                    System.out.println("enter idle");
                    LOGGER.warn("Idle condition "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                    idleManager.watch(f);
                    setIsConnected(true);
                    LOGGER.warn("Folder.watch "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                    DbUtil.updateMediaStream(getIdentifier(), true);
                    isConnecting.set(false);
                    connectionAttempt.set(0);
                    LOGGER.warn("Db updated "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                    System.out.println("idle done");
                    scheduleTask();
                    LOGGER.warn("Scheduler starts "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
                }

            }

            System.out.println(new Date()
                    + "================================== connect() end");
            LOGGER.warn("____________________END connecting "+getIdentifier() +"on Imap"+imapLoginId+" pw "+imapPasswrd);
        } catch (Exception fex) {

                if (connectionAttempt.get() >= 3) {
                    LOGGER.error("Reconnection Failed", fex);
                    connectionAttempt.set(0);
                    throw new IllegalStateException(
                            "Error while connecting mediaStream", fex);
                }
                LOGGER.error("Reconnecting mediastream attempt "
                        + connectionAttempt.get(), fex);
                connectionAttempt.incrementAndGet();
                closeFolderAndStore();
                connect();

        }

The email is received here, the "public void messagesAdded(MessageCountEvent ev)" method calling twice

@Override
    public void messagesAdded(MessageCountEvent ev) {
        Message[] msgs = ev.getMessages();
        System.out.println("Got " + msgs.length + " new messages");
        LOGGER.info("New message received {}" ,msgs.length);
        // Just dump out the new messages

        for (int i = 0; i < msgs.length; i++) {

            try {

                // msgs[i].writeTo(System.out);

                    System.out.println("-----");
                    System.out.println("Message >>" + msgs[i].getMessageNumber()
                            + ":");
                    LOGGER.info("Email Received >>> {}" , msgs[i].getMessageNumber());
                    processEmail(msgs[i]);


            } catch (Exception ioex) {
                LOGGER.error("messagesAdded:getmessagenumber", ioex);
                ioex.printStackTrace();
            }
        }
        try {
            synchronized (folder) {
                if (folder != null && idleManager != null) {
                    idleManager.watch(folder);
                }
            }
        } catch (Exception e) {
            LOGGER.error("messagesAdded:watch", e);
            e.printStackTrace();
        }
    }

The IMAP debug log is follows

DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: enable STARTTLS
DEBUG IMAP: closeFoldersOnStoreFailure
DEBUG IMAP: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [QQBNADMAUABSADAANQBDAEEAMAAyADgALgBlAHUAcgBwAHIAZAAwADUALgBwAHIAbwBkAC4AbwB1AHQAbABvAG8AawAuAGMAbwBtAA==]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: XOAUTH2
DEBUG IMAP: protocolConnect login, host=outlook.office365.com, [email protected], password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK AUTHENTICATE completed.
A2 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+
A2 OK CAPABILITY completed.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: XOAUTH2
A3 LIST "" inbox
DEBUG IMAP: IdleManager select starting
DEBUG IMAP: IdleManager waiting...
* LIST (\Marked \HasNoChildren) "/" INBOX
A3 OK LIST completed.
DEBUG IMAP: connection available -- size: 1
A4 SELECT INBOX
* 463 EXISTS
* 0 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
* OK [UNSEEN 36] Is the first unseen message
* OK [UIDVALIDITY 14] UIDVALIDITY value
* OK [UIDNEXT 7207] The next unique identifier value
A4 OK [READ-WRITE] SELECT completed.
DEBUG IMAP: IdleManager watching imap://amith.bharathan%[email protected]/INBOX
A5 IDLE
+ IDLE accepted, awaiting DONE command.
DEBUG IMAP: startIdle: set to IDLE
DEBUG IMAP: startIdle: return true
DEBUG IMAP: IdleManager.watch startIdle succeeded for imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager selected 0 channels
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
* 1 RECENT
* 464 EXISTS
DEBUG IMAP: waitIfIdle: request IdleManager to abort
DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[pool-21-thread-2,5,main]
DEBUG IMAP: IdleManager continue watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager aborting IDLE for folder: imap://amith.bharathan%[email protected]/INBOX
DONE
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
A5 OK IDLE completed.
DEBUG IMAP: handleIdle: set to RUNNING
DEBUG IMAP: waitIfIdle: wait done, idleState 0: Thread[pool-21-thread-2,5,main]
A6 FETCH 464 (ENVELOPE INTERNALDATE RFC822.SIZE)
DEBUG IMAP: IdleManager done watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager waiting...
* 464 FETCH (ENVELOPE ("Wed, 29 Jun 2016 08:31:06 +0000" "4444" (("Amith K Bharathan" NIL "amith.bharathan" "drd.co.in")) NIL NIL (("Amith K Bharathan" NIL "amith.bharathan" "drd.co.in")) NIL NIL NIL "<HE1PR06MB1196D2A9F2FDB040AB2B05B5BF230@HE1PR06MB1196.eurprd06.prod.outlook.com>") INTERNALDATE "29-Jun-2016 14:01:07 +0530" RFC822.SIZE 19822)
A6 OK FETCH completed.
A7 FETCH 464 (BODYSTRUCTURE)
* 464 FETCH (BODYSTRUCTURE ("text" "html" ("charset" "us-ascii") NIL NIL "7BIT" 1586 48 NIL NIL "en-US" NIL))
A7 OK FETCH completed.
A8 FETCH 464 (BODY[TEXT]<0.1586>)
* 464 FETCH (BODY[TEXT]<0> {1586}
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0in;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:purple;
    text-decoration:underline;}
span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:"Calibri","sans-serif";
    color:windowtext;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-family:"Calibri","sans-serif";}
@page WordSection1
    {size:8.5in 11.0in;
    margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">555555555555<o:p></o:p></p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A8 OK FETCH completed.
A9 FETCH 464 (BODY[TEXT]<0.1586>)
* 464 FETCH (BODY[TEXT]<0> {1586}
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0in;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:purple;
    text-decoration:underline;}
span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:"Calibri","sans-serif";
    color:windowtext;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-family:"Calibri","sans-serif";}
@page WordSection1
    {size:8.5in 11.0in;
    margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">555555555555<o:p></o:p></p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A9 OK FETCH completed.
DEBUG IMAP: IdleManager watching imap://amith.bharathan%[email protected]/INBOX
A10 IDLE
+ IDLE accepted, awaiting DONE command.
DEBUG IMAP: startIdle: set to IDLE
DEBUG IMAP: startIdle: return true
DEBUG IMAP: IdleManager.watch startIdle succeeded for imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager selected 0 channels
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
* 2 RECENT
* 465 EXISTS
DEBUG IMAP: waitIfIdle: request IdleManager to abort
DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[pool-21-thread-2,5,main]
DEBUG IMAP: IdleManager continue watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager aborting IDLE for folder: imap://amith.bharathan%[email protected]/INBOX
DONE
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
A10 OK IDLE completed.
DEBUG IMAP: handleIdle: set to RUNNING
DEBUG IMAP: waitIfIdle: wait done, idleState 0: Thread[pool-21-thread-2,5,main]
A11 FETCH 465 (ENVELOPE INTERNALDATE RFC822.SIZE)
DEBUG IMAP: IdleManager done watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager waiting...
* 465 FETCH (ENVELOPE ("Wed, 29 Jun 2016 08:32:01 +0000" "test" (("Gopikrishnan V" NIL "gopikrishnan.v" "drd.co.in")) NIL NIL (("Amith K Bharathan" NIL "amith.bharathan" "drd.co.in")) NIL NIL NIL "<DB4PR06MB0464157DB21114B7E237B93C0230@DB4PR06MB046.eurprd06.prod.outlook.com>") INTERNALDATE "29-Jun-2016 14:02:05 +0530" RFC822.SIZE 18300)
A11 OK FETCH completed.
A12 FETCH 465 (BODYSTRUCTURE)
* 465 FETCH (BODYSTRUCTURE ("text" "html" ("charset" "iso-8859-1") NIL NIL "quoted-printable" 427 15 NIL NIL "en-GB" NIL))
A12 OK FETCH completed.
A13 FETCH 465 (BODY[TEXT]<0.427>)
* 465 FETCH (BODY[TEXT]<0> {427}
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;back=
ground-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>test<br>
</p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A13 OK FETCH completed.
A14 FETCH 465 (BODY[TEXT]<0.427>)
* 465 FETCH (BODY[TEXT]<0> {427}
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;back=
ground-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>test<br>
</p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A14 OK FETCH completed.
DEBUG IMAP: IdleManager watching imap://amith.bharathan%[email protected]/INBOX
A15 IDLE
+ IDLE accepted, awaiting DONE command.
DEBUG IMAP: startIdle: set to IDLE
DEBUG IMAP: startIdle: return true
DEBUG IMAP: IdleManager.watch startIdle succeeded for imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager selected 0 channels
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
* 3 RECENT
* 466 EXISTS
DEBUG IMAP: waitIfIdle: request IdleManager to abort
DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[pool-21-thread-2,5,main]
DEBUG IMAP: IdleManager continue watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager aborting IDLE for folder: imap://amith.bharathan%[email protected]/INBOX
DONE
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
A15 OK IDLE completed.
DEBUG IMAP: handleIdle: set to RUNNING
DEBUG IMAP: waitIfIdle: wait done, idleState 0: Thread[pool-21-thread-2,5,main]
A16 FETCH 466 (ENVELOPE INTERNALDATE RFC822.SIZE)
DEBUG IMAP: IdleManager done watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager waiting...
* 466 FETCH (ENVELOPE ("Wed, 29 Jun 2016 08:35:56 +0000" NIL (("Gopikrishnan V" NIL "gopikrishnan.v" "drd.co.in")) NIL NIL (("Amith K Bharathan" NIL "amith.bharathan" "drd.co.in")) NIL NIL NIL "<DB4PR06MB0466336D31C8CE8919BD4B3C0230@DB4PR06MB046.eurprd06.prod.outlook.com>") INTERNALDATE "29-Jun-2016 14:05:57 +0530" RFC822.SIZE 11747)
A16 OK FETCH completed.
A17 FETCH 466 (BODYSTRUCTURE)
* 466 FETCH (BODYSTRUCTURE ("text" "html" ("charset" "iso-8859-1") NIL NIL "quoted-printable" 427 15 NIL NIL "en-GB" NIL))
A17 OK FETCH completed.
A18 FETCH 466 (BODY[TEXT]<0.427>)
* 466 FETCH (BODY[TEXT]<0> {427}
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;back=
ground-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>test<br>
</p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A18 OK FETCH completed.
A19 FETCH 466 (BODY[TEXT]<0.427>)
* 466 FETCH (BODY[TEXT]<0> {427}
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;back=
ground-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>test<br>
</p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A19 OK FETCH completed.
DEBUG IMAP: IdleManager watching imap://amith.bharathan%[email protected]/INBOX
A20 IDLE
+ IDLE accepted, awaiting DONE command.
DEBUG IMAP: startIdle: set to IDLE
DEBUG IMAP: startIdle: return true
DEBUG IMAP: IdleManager.watch startIdle succeeded for imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager selected 0 channels
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
* 466 EXPUNGE
* 466 EXISTS
DEBUG IMAP: waitIfIdle: request IdleManager to abort
DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[pool-21-thread-2,5,main]
DEBUG IMAP: IdleManager continue watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager aborting IDLE for folder: imap://amith.bharathan%[email protected]/INBOX
DONE
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: imap://amith.bharathan%[email protected]/INBOX
A20 OK IDLE completed.
DEBUG IMAP: handleIdle: set to RUNNING
DEBUG IMAP: waitIfIdle: wait done, idleState 0: Thread[pool-21-thread-2,5,main]
A21 FETCH 466 (ENVELOPE INTERNALDATE RFC822.SIZE)
DEBUG IMAP: IdleManager done watching folder imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager waiting...
* 466 FETCH (ENVELOPE ("Wed, 29 Jun 2016 08:35:56 +0000" NIL (("Gopikrishnan V" NIL "gopikrishnan.v" "drd.co.in")) NIL NIL (("Amith K Bharathan" NIL "amith.bharathan" "drd.co.in")) NIL NIL NIL "<DB4PR06MB0466336D31C8CE8919BD4B3C0230@DB4PR06MB046.eurprd06.prod.outlook.com>") INTERNALDATE "29-Jun-2016 14:05:57 +0530" RFC822.SIZE 13152)
A21 OK FETCH completed.
A22 FETCH 466 (BODYSTRUCTURE)
* 466 FETCH (BODYSTRUCTURE ("text" "html" ("charset" "iso-8859-1") NIL NIL "quoted-printable" 427 15 NIL NIL "en-GB" NIL))
A22 OK FETCH completed.
A23 FETCH 466 (BODY[TEXT]<0.427>)
* 466 FETCH (BODY[TEXT]<0> {427}
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;back=
ground-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>test<br>
</p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A23 OK FETCH completed.
A24 FETCH 466 (BODY[TEXT]<0.427>)
* 466 FETCH (BODY[TEXT]<0> {427}
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;back=
ground-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>test<br>
</p>
</div>
</body>
</html>
 FLAGS (\Seen \Recent))
A24 OK FETCH completed.
DEBUG IMAP: IdleManager watching imap://amith.bharathan%[email protected]/INBOX
A25 IDLE
+ IDLE accepted, awaiting DONE command.
DEBUG IMAP: startIdle: set to IDLE
DEBUG IMAP: startIdle: return true
DEBUG IMAP: IdleManager.watch startIdle succeeded for imap://amith.bharathan%[email protected]/INBOX
DEBUG IMAP: IdleManager selected 0 channels
DEBUG IMAP: IdleManager adding imap://amith.bharathan%[email protected]/INBOX to selector
DEBUG IMAP: IdleManager waiting...

The email with body "test" is repeating

Please advice.

Thanks

Amith

Upvotes: 0

Views: 420

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29961

This is message number 466 in the mailbox. The server announces the new message:

    * 3 RECENT
    * 466 EXISTS

You process the message:

    A16 FETCH 466 (ENVELOPE INTERNALDATE RFC822.SIZE)
    ...

You wait for new messages, and the server tells you:

    * 466 EXPUNGE
    * 466 EXISTS

That means message number 466 was completely removed from the mailbox and there's a new message number 466 in the mailbox. JavaMail notifies you of the new message number 466. Apparently, it's exactly the same as the removed message number 466.

I have no idea why the server is doing that. It looks like a bug in the server to me. Please report it to Microsoft.

You might want to check the IMAP UID of the messages that you're processing. The new message 466 absolutely must have a different UID. If it doesn't, that's an even more serious bug in the server.

You can also check the Message-ID of the message to make sure you haven't seen that message before. If all the error cases are like this, you may only need to keep a history of one message. But note that while Message-IDs are intended to be unique and are usually unique, unlike IMAP UIDs there's no guarantee that they are unique, or even that they exist at all.

Upvotes: 1

Related Questions