Java
Chilkat2 Java
Fetch a Single Message's MIME into a StringBuilder
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.
Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw
8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MessageSet;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
// StringBuilder. The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
// number, and the 3rd is the StringBuilder (cleared first).
Imap imap = new Imap();
imap.setSsl(true);
imap.setPort(993);
try {
imap.connect("imap.example.com");
imap.login("user@example.com", "myPassword");
imap.selectMailbox("Inbox");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Search for the message ids to download, returning UIDs.
boolean wantUids = true;
MessageSet msgSet = new MessageSet();
try {
imap.queryMbx("ALL", wantUids, msgSet);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
if (msgSet.getCount() > 0) {
long uid = msgSet.getId(0);
// Download the message's MIME into a StringBuilder. The id is a UID.
boolean useUid = true;
StringBuilder sbMime = new StringBuilder();
try {
imap.fetchSingleAsMimeSb(uid, useUid, sbMime);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("MIME length: " + sbMime.getLength() + " chars");
}
try {
imap.disconnect();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
}
}