Java Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Java Examples

Quick Start
Java Unicode
Java Certs
Java Email
Java Encryption
Java FTP
HTML-to-XML
Java HTTP
Java IMAP
Java MHT
Java MIME
Java RSA
Java S/MIME
Java Signatures
Java Socket
Java Spider
Java Tar
Java Upload
Java XML
Java XMP
Java Zip

More Examples...
Email Object
POP3
SMTP
RSS
Atom
String
Byte Array
Self-Extractor

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

Read S/MIME Encrypted Email

Read S/MIME encrypted email.

Download Chilkat Java Library

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkImap imap = new CkImap();

    boolean success;

    //  Anything unlocks the component and begins a fully-functional 30-day trial.
    success = imap.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        System.out.println(imap.lastErrorText());
        return;
    }

    //  Connect to an IMAP server.
    success = imap.Connect("mail.chilkatsoft.com");
    if (success != true) {
        System.out.println(imap.lastErrorText());
        return;
    }

    //  Login
    success = imap.Login("myLogin","myPassword");
    if (success != true) {
        System.out.println(imap.lastErrorText());
        return;
    }

    //  Select an IMAP mailbox
    success = imap.SelectMailbox("Inbox");
    if (success != true) {
        System.out.println(imap.lastErrorText());
        return;
    }

    //  The AutoUnwrapSecurity method controls whether signed/encrypted emails are automatically
    //  decrypted and/or verified.

    //  When set to true, which is the default, security envelopes are automatically "unwrapped"
    //  when a message is retrieved from the server. Signed emails are automatically verified, and
    //  encrypted emails are automatically decrypted, restoring the email to the original state before
    //  signing and/or encrypting. Information about the signing and encrypting certificates can be
    //  retrieved from the Email object (methods: GetSignedByCert, GetEncryptedByCert;
    //  properties: SignedBy, EncryptedBy, SignaturesValid, Decrypted, ReceivedSigned,
    //  ReceivedEncrypted).

    //  This example will explicity set AutoUnwrapSecurity to true, even though it's
    //  already the default value:
    imap.put_AutoUnwrapSecurity(true);

    //  The NumMessages property contains the number of messages in the selected mailbox.
    int numToFetch;
    numToFetch = imap.get_NumMessages();
    //  Download all the email in the Inbox.
    CkEmailBundle bundle;
    bundle = imap.FetchSequence(1,numToFetch);
    if (bundle == null ) {
        System.out.println(imap.lastErrorText());
        return;
    }

    //  Loop over the bundle,
    int i;
    for (i = 0; i <= bundle.get_MessageCount() - 1; i++) {
        CkEmail email;
        email = bundle.GetEmail(i);

        System.out.println(email.ck_from());
        System.out.println(email.subject());

        //  At this point, if the email was signed and/or encrypted, it is already "unwrapped", i.e.
        //  the email is already decrypted and in a state as if it were never signed or encrypted.
        //  You may check to see if the email was received encrypted or signed, and if so,
        //  whether it was successfully unwrapped and who signed or encrypted it:
        if (email.get_ReceivedEncrypted() == true) {

            System.out.println("This email was encrypted when received.");
            if (email.get_Decrypted() == true) {
                System.out.println("This email was successfully decrypted.  It was encrypted by:");
                System.out.println(email.encryptedBy());
            }
            else {
                System.out.println("This email was not decrypted.");
            }

        }

        if (email.get_ReceivedSigned() == true) {

            System.out.println("This email was signed when received.");
            if (email.get_SignaturesValid() == true) {
                System.out.println("The signature was verified.  It was signed by:");
                System.out.println(email.signedBy());
            }
            else {
                System.out.println("The signature verification failed.");
            }

        }

        //  The email's body, HTML body, attachments, etc.
        //  are decrypted and available just like any non-encrypted email.

        System.out.println("--");

    }

    //  Disconnect from the IMAP server.
    imap.Disconnect();


  }
}

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.