Java Requires Chilkat v11.0.0+
Chilkat2 Java
How to Download Messages in MessageSet One at a Time
See more IMAP Examples
If a message set contains a huge number of emails, it's NOT a good idea to try to download all at once into an email bundle using a method such as FetchBundle. It's better to iterate over the messages in the set to download one by one.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MessageSet;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Imap imap = new Imap();
// Connect using TLS.
imap.setSsl(true);
imap.setPort(993);
try {
imap.connect("imap.example.com");
// Authenticate
imap.login("email_account_login", "email_account_password");
// Select an IMAP mailbox
imap.selectMailbox("Inbox");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Search for messages and return a set of matching messages.
// (This example will simply search for ALL messages.)
boolean fetchUids = true;
MessageSet messageSet = new MessageSet();
try {
imap.queryMbx("ALL", fetchUids, messageSet);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Number of messages = " + messageSet.getCount());
Email email = new Email();
int i = 0;
while (i < messageSet.getCount()) {
try {
imap.fetchEmail(false, messageSet.getId(i), fetchUids, email);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(email.getFrom() + "; " + email.getSubject());
i = i + 1;
}
System.out.println("OK");
}
}