Dart Requires Chilkat v11.0.0+
Dart
Using Client Certificate w/ IMAP SSL
Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final imap = CkImap();
// To use a secure SSL connection, set SSL and the port:
imap.ssl = true;
// The typical port for IMAP SSL is 993
imap.port = 993;
// Load a certificate from a PFX file and use it.
// Note: Other methods are available to load pre-installed
// certificates from registry-based certificate stores.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
final certStore = CkCertStore();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
try {
certStore.loadPfxFile('myCertWithPrivateKey.pfx', 'secret');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Find the certificate by the subject common name:
final jsonCN = CkJsonObject();
jsonCN.updateString('CN', 'cert common name');
final cert = CkCert();
try {
certStore.findCert(jsonCN, cert);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// If a PFX file is known to contain a single certificate,
// you may load it directly into a Chilkat certificate object.
// This snippet of source code shows how:
final cert2 = CkCert();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
try {
cert2.loadPfxFile('myClientCert.pfx', 'secret');
} on ChilkatException {
print(cert.lastErrorText);
return;
}
// Use the cert:
imap.setSslClientCert(cert);
// Connect to an IMAP server.
try {
imap.connect('imap.example.com');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Login
try {
imap.login('myLogin', 'myPassword');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Select an IMAP mailbox
try {
imap.selectMailbox('Inbox');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Get the message IDs of all the emails in the mailbox
final fetchUids = true;
final messageSet = CkMessageSet();
try {
imap.queryMbx('ALL', fetchUids, messageSet);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Fetch the emails into a bundle object:
final bundle = CkEmailBundle();
final headersOnly = false;
try {
imap.fetchMsgSet(headersOnly, messageSet, bundle);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Loop over the bundle and display the FROM and SUBJECT of each.
final email = CkEmail();
var i = 0;
final numEmails = bundle.messageCount;
while (i < numEmails) {
bundle.emailAt(i, email);
print(email.from);
print(email.subject);
print('--');
i++;
}
// Disconnect from the IMAP server.
imap.disconnect();
}