Java
Chilkat2 Java
Copy Email from one IMAP Account to Another
See more IMAP Examples
Demonstrates how to copy the email in a mailbox from one account to another.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.FileAccess;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MessageSet;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
Imap imapSrc = new Imap();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Connect to our source IMAP server.
imapSrc.setSsl(true);
imapSrc.setPort(993);
try {
imapSrc.connect("MY-IMAP-DOMAIN");
// Login to the source IMAP server
imapSrc.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Imap imapDest = new Imap();
// Connect to our destination IMAP server.
imapDest.setSsl(true);
imapDest.setPort(993);
try {
imapDest.connect("MY-IMAP-DOMAIN2");
// Login to the destination IMAP server
imapDest.login("MY-IMAP-LOGIN2", "MY-IMAP-PASSWORD2");
// Select a source IMAP mailbox on the source IMAP server
imapSrc.selectMailbox("Inbox");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get the set of UIDs for all emails on the source server.
ERROR: Method not in Chilkat2 Java: Imap.Search
MessageSet mset = imapSrc.ERROR();
if (!imapSrc.getLastMethodSuccess()) {
System.out.println(imapSrc.getLastErrorText());
return;
}
// Load the complete set of UIDs that were previously copied.
// We dont' want to copy any of these to the destination.
FileAccess fac = new FileAccess();
MessageSet msetAlreadyCopied = new MessageSet();
String strMsgSet;
try {
strMsgSet = fac.readEntireTextFile("qa_cache/saAlreadyLoaded.txt", "utf-8");
} catch (ChilkatException e) {
strMsgSet = null;
}
if (strMsgSet != null) {
msetAlreadyCopied.fromCompactString(strMsgSet);
}
int numUids = mset.getCount();
StringBuilder sbFlags = new StringBuilder();
int i = 0;
while (i < numUids) {
// If this UID was not already copied...
int uid = (int) mset.getId(i);
if (!msetAlreadyCopied.containsId(uid)) {
System.out.println("copying " + uid + "...");
// Get the flags.
String flags;
String mimeStr;
try {
flags = imapSrc.fetchFlags(uid, true);
sbFlags.setString(flags);
// Get the MIME of this email from the source.
mimeStr = imapSrc.fetchSingleAsMime(uid, true);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
boolean seen = sbFlags.contains("\\Seen", false);
boolean flagged = sbFlags.contains("\\Flagged", false);
boolean answered = sbFlags.contains("\\Answered", false);
boolean draft = sbFlags.contains("\\Draft", false);
try {
imapDest.appendMimeWithFlags("Inbox", mimeStr, seen, flagged, answered, draft);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Update msetAlreadyCopied with the uid just copied.
msetAlreadyCopied.insertId(uid);
// Save at every iteration just in case there's a failure..
strMsgSet = msetAlreadyCopied.toCompactString();
fac.writeEntireTextFile("qa_cache/saAlreadyLoaded.txt", strMsgSet, "utf-8", false);
}
i = i + 1;
}
mset.close();
// Disconnect from the IMAP servers.
imapSrc.disconnect();
imapDest.disconnect();
}
}