Dart
Dart
Send Encrypted Email to Multiple Recipients
Demonstrates how to create and send an S/MIME encrypted email to multiple recipients. The digital certificate of each recipient is required. The encrypting/sending process uses each recipient's digital certificate (which internally contains the public key). Each recipient decrypts the received email using his/her private key.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for sending and receiving email.
final mailman = CkMailMan();
// Set the SMTP server.
mailman.smtpHost = 'smtp.mymailserver.com';
// Load each recipient's certificate into a Chilkat certificate object.
// This example loads the certificates from files. However, the Chilkat
// certificate object provides other means for loading certificates,
// such as from in-memory PEM strings, or in-memory binary DER encoded form, etc.
final cert1 = CkCert();
success = true;
try {
cert1.loadFromFile('recipient1.cer');
} on ChilkatException {
success = false;
}
if (!success) {
print(cert1.lastErrorText);
return;
}
final cert2 = CkCert();
success = true;
try {
cert2.loadFromFile('recipient2.cer');
} on ChilkatException {
success = false;
}
if (!success) {
print(cert2.lastErrorText);
return;
}
final cert3 = CkCert();
success = true;
try {
cert3.loadFromFile('recipient3.cer');
} on ChilkatException {
success = false;
}
if (!success) {
print(cert3.lastErrorText);
return;
}
// Create a new email object
final email = CkEmail();
email.subject = 'This email is encrypted and sent to 3 recipients';
email.body = 'This is an S/MIME encrypted mail sent to 3 recipients';
email.from = 'Chilkat Support <support@chilkatsoft.com>';
// Make each of the certificates available for encrypting the email
// by calling AddEncryptCert for each.
success = true;
try {
email.addEncryptCert(cert1);
} on ChilkatException {
success = false;
}
if (success) {
success = true;
try {
email.addEncryptCert(cert2);
} on ChilkatException {
success = false;
}
}
if (success) {
success = true;
try {
email.addEncryptCert(cert3);
} on ChilkatException {
success = false;
}
}
if (!success) {
print(email.lastErrorText);
return;
}
// Add 3 recipients to the email (2 TO addresses, and 1 CC address)
email.addTo('Recipient 1', 'admin@cknotes.com');
email.addTo('Recipient 2', 'somebody001122@yahoo.com');
success = true;
try {
email.addCC('Recipient 3', 'somebody123xyz@gmail.com');
} on ChilkatException {
success = false;
}
// Indicate that the email is to be sent encrypted.
email.sendEncrypted = true;
// Send the encrypted email...
success = true;
try {
mailman.sendEmail(email);
} on ChilkatException {
success = false;
}
if (!success) {
print(mailman.lastErrorText);
} else {
print('Encrypted Email Sent!');
}
}