Sample code for 30+ languages & platforms
Java

Append a MIME Message to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMime method, which uploads a complete RFC 822/MIME message to a mailbox. The first argument is the destination mailbox name and the second is the MIME text. This example obtains the MIME from an Email object and uploads it to Drafts.

Background: Use AppendMime when you already hold a message's raw source — a .eml file, a message you assembled, or one you received. Chilkat sends the text exactly as supplied, without normalizing line endings or re-encoding headers, so every header and MIME boundary is preserved verbatim. The supplied text must be plain text (no raw non-text binary bytes).

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Imap;

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the Imap.AppendMime method, which uploads a complete MIME message to a mailbox.
        // The 1st argument is the destination mailbox name and the 2nd is the MIME text.

        Imap imap = new Imap();

        imap.setSsl(true);
        imap.setPort(993);

        try {
            imap.connect("imap.example.com");

            imap.login("user@example.com", "myPassword");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Build the email to be uploaded.
        Email email = new Email();
        email.setSubject("Meeting agenda");
        email.setFrom("Alice <alice@example.com>");
        email.addTo("Bob", "bob@example.com");
        email.setBody("Let's meet at 10am to review the agenda.");

        // Obtain the MIME text to upload.  Here it is generated from the Email, but it could equally
        // be read from a .eml file.
        String mimeText;
        try {
            mimeText = email.getMime();

            // Append (upload) the MIME to the "Drafts" mailbox.
            imap.appendMime("Drafts", mimeText);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Appended UID: " + imap.getAppendUid());

        try {
            imap.disconnect();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }
    }
}