Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Android™ Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Android™) IMAP Read Encrypted Email

See more IMAP Examples

Demonstrates how to read encrypted email from an IMAP mailbox.

Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

Information about the original encrypted state of the email is available after it has been downloaded and decrypted.

Note: This example requires Chilkat v10.1.2 or later because it uses the Secrets class.

Chilkat Android™ Downloads

Android™ Java Libraries

Android C/C++ Libraries

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CkImap imap = new CkImap();

    imap.put_Ssl(true);
    imap.put_Port(993);
    boolean success = imap.Connect("imap.example2.com");
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
    // See how we originally saved the email credentials to the Keychain here:
    // Save Email Credentials in Apple Keychain or Windows Credentials Manager
    CkSecrets secrets = new CkSecrets();

    // On Windows, this is the Windows Credentials Manager
    // On MacOS/iOS, it is the Apple Keychain
    secrets.put_Location("local_manager");

    // Specify the name of the secret.
    // service and username are required.
    // appName and domain are optional.
    // Note: The values are arbitrary and can be anything you want.
    CkJsonObject json = new CkJsonObject();
    json.UpdateString("appName","MyEmailApp");
    json.UpdateString("service","IMAP");
    json.UpdateString("domain","example2.com");
    json.UpdateString("username","jane@example2.com");

    String password = secrets.getSecretStr(json);
    if (secrets.get_LastMethodSuccess() == false) {
        Log.i(TAG, secrets.lastErrorText());
        return;
        }

    success = imap.Login("jane@example2.com",password);
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Select an IMAP mailbox
    success = imap.SelectMailbox("Inbox");
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // This example: Send Encrypted Email using Certificate in Apple Keychain
    // sent an email with the subject "This email is encrypted".
    // Let's download an email with the word "encrypted" in the subject.
    CkMessageSet messageSet = imap.Search("SUBJECT encrypted",true);
    if (imap.get_LastMethodSuccess() == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    if (messageSet.get_Count() == 0) {
        Log.i(TAG, "No messages found.");

        return;
        }

    // Reading encrypted email works the same as reading non-encrypted email. 
    // If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), 
    // Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

    int uid = messageSet.GetId(0);
    CkEmail email = imap.FetchSingle(uid,true);
    if (imap.get_LastMethodSuccess() == false) {
        Log.i(TAG, imap.lastErrorText());

        return;
        }

    // Here we can show if the email was received encrypted, if it was successfully decrypted, and
    // which certificate was used to decrypt.
    Log.i(TAG, "Email received encrypted: " + String.valueOf(email.get_ReceivedEncrypted()));
    // Was it successfully decrypted?
    Log.i(TAG, "Successfully decrypted: " + String.valueOf(email.get_Decrypted()));
    // What cert was used to decrypt?
    Log.i(TAG, "Encrypted by: " + email.encryptedBy());
    CkCert cert = email.GetEncryptedByCert();
    if (email.get_LastMethodSuccess() == true) {
        Log.i(TAG, "Certificate DN: " + cert.subjectDN());

        }

    // Show the decrypted email body.
    Log.i(TAG, email.body());

    imap.Disconnect();

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

 

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