Java
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.MailMan;
public class ChilkatExample {
public static void main(String[] args) {
// 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.
MailMan mailman = new MailMan();
// Set the SMTP server.
mailman.setSmtpHost("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.
Cert cert1 = new Cert();
try {
cert1.loadFromFile("recipient1.cer");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Cert cert2 = new Cert();
try {
cert2.loadFromFile("recipient2.cer");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Cert cert3 = new Cert();
try {
cert3.loadFromFile("recipient3.cer");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Create a new email object
Email email = new Email();
email.setSubject("This email is encrypted and sent to 3 recipients");
email.setBody("This is an S/MIME encrypted mail sent to 3 recipients");
email.setFrom("Chilkat Support <support@chilkatsoft.com>");
// Make each of the certificates available for encrypting the email
// by calling AddEncryptCert for each.
try {
email.addEncryptCert(cert1);
email.addEncryptCert(cert2);
email.addEncryptCert(cert3);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
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");
email.addCC("Recipient 3", "somebody123xyz@gmail.com");
// Indicate that the email is to be sent encrypted.
email.setSendEncrypted(true);
// Send the encrypted email...
try {
mailman.sendEmail(email);
System.out.println("Encrypted Email Sent!");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
}
}
}