'IMAP imap too many simultaneous connections
I am using imap-simple to access a mailbox. I have the following javascript code to read IMAP Mailbox.
async getLastIncomingEmail() {
//the imap config
const imapEmailConfig = {
imap: {
user: existingEmail,
password: existingPassword,
host: 'imap.ethereal.email',
port: 993,
tls: true,
authTimeout: 10000,
}
};
flag = true
i = 1
while (flag) {
try {
//console.log(imapEmailConfig)
const connection = await imaps.connect(imapEmailConfig)
// Open a mailbox, and look at the 50 newest messages
await connection.openBox('INBOX')
const searchCriteria = [
// "UNSEEN",
'1:50']
const fetchOptions = {
bodies: [''],
}
delay(1000)
console.log('delaying for 1 second')
const messages = await connection.search(searchCriteria, fetchOptions)
// and close the connection to avoid it hanging
connection.end()
if (!messages.length) {
console.log(`can not find new emails, nr of tries ${i}`)
i += 1
if (i === 90) {
flag = false
return null
}
} else {
flag = false
console.log('there are %d messages', messages.length)
const mail = await simpleParser(
messages[messages.length - 1].parts[0].body,
)
/**
* It's possible to access these objects in the selected mail
* the following logs will be printed in Intellij Console to better understand the functionality
*/
console.log(`Found following email : from ${mail.from.text}\n
from:${mail.to.text}\n
subject:${mail.subject}\n
date:${mail.date}\n
attachments:${mail.attachments}`)
return {
from: mail.from.text,
to: mail.to.text,
subject: mail.subject,
html: mail.html,
attachments: mail.attachments,
date: mail.date,
}
}
} catch (e) {
console.error(e)
return null
}
}
}
the problem with the following code is that the connection is not closed. after running the test multiple times I get map too many simultaneous connections.
when I modify the code as follows
async getLastIncomingEmail() {
//the imap config
const imapEmailConfig = {
imap: {
user: existingEmail,
password: existingPassword,
host: 'imap.ethereal.email',
port: 993,
tls: true,
authTimeout: 10000,
}
};
flag = true
i = 1
//console.log(imapEmailConfig)
const connection = await imaps.connect(imapEmailConfig)
while (flag) {
try {
// Open a mailbox, and look at the 50 newest messages
await connection.openBox('INBOX')
const searchCriteria = [
// "UNSEEN",
'1:50']
const fetchOptions = {
bodies: [''],
}
delay(1000)
console.log('delaying for 1 second')
const messages = await connection.search(searchCriteria, fetchOptions)
// and close the connection to avoid it hanging
connection.end()
if (!messages.length) {
console.log(`can not find new emails, nr of tries ${i}`)
i += 1
if (i === 90) {
flag = false
return null
}
} else {
flag = false
console.log('there are %d messages', messages.length)
const mail = await simpleParser(
messages[messages.length - 1].parts[0].body,
)
/**
* It's possible to access these objects in the selected mail
* the following logs will be printed in Intellij Console to better understand the functionality
*/
console.log(`Found following email : from ${mail.from.text}\n
from:${mail.to.text}\n
subject:${mail.subject}\n
date:${mail.date}\n
attachments:${mail.attachments}`)
return {
from: mail.from.text,
to: mail.to.text,
subject: mail.subject,
html: mail.html,
attachments: mail.attachments,
date: mail.date,
}
}
} catch (e) {
console.error(e)
return null
}
}
}
no results will be fetched. How do I close the connection properly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
