Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Send Encrypted Email using Certificate in Apple KeychainSee more Apple Keychain ExamplesSend encrypted (S/MIME) email using a certificate found in the Apple Keychain. Note: This example requires Chilkat v10.1.2 or later.
IncludeFile "CkCert.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkSecrets.pb" IncludeFile "CkEmail.pb" IncludeFile "CkMailMan.pb" Procedure ChilkatExample() email.i = CkEmail::ckCreate() If email.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkEmail::setCkSubject(email, "This email is encrypted") CkEmail::setCkBody(email, "This is a S/MIME encrypted mail") CkEmail::setCkFrom(email, "Joe <joe@example.com>") ; Emails are encrypted using the recipient's certificate. recipientEmailAddr.s = "jane@example2.com" CkEmail::ckAddTo(email,"",recipientEmailAddr) ; Indicate that the email is to be sent encrypted. CkEmail::setCkSendEncrypted(email, 1) cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; ----------------------------------------------- ; This example requires Chilkat v10.1.2 or later. ; ----------------------------------------------- ; The recipient's certificate is used to encrypt. ; This will load the certificate from the Apple Keychain. success.i = CkCert::ckLoadByEmailAddress(cert,recipientEmailAddr) If success <> 1 Debug CkCert::ckLastErrorText(cert) CkEmail::ckDispose(email) CkCert::ckDispose(cert) ProcedureReturn EndIf ; Specify the certificate to be used for encryption. success = CkEmail::ckSetEncryptCert(email,cert) CkEmail::setCkSendEncrypted(email, 1) mailman.i = CkMailMan::ckCreate() If mailman.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkMailMan::setCkSmtpHost(mailman, "smtp.example.com") CkMailMan::setCkSmtpUsername(mailman, "joe@example.com") CkMailMan::setCkSmtpPort(mailman, 587) CkMailMan::setCkStartTLS(mailman, 1) ; We'll get the SMTP password from the Apple Keychain. ; See how we originally saved the email credentials to the Keychain here: ; Save Email Credentials in Apple Keychain or Windows Credentials Manager secrets.i = CkSecrets::ckCreate() If secrets.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; On Windows, this is the Windows Credentials Manager ; On MacOS/iOS, it is the Apple Keychain CkSecrets::setCkLocation(secrets, "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. json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(json,"appName","MyEmailApp") CkJsonObject::ckUpdateString(json,"service","SMTP") CkJsonObject::ckUpdateString(json,"domain","example.com") CkJsonObject::ckUpdateString(json,"username","joe@example.com") CkMailMan::setCkSmtpPassword(mailman, CkSecrets::ckGetSecretStr(secrets,json)) ; Send the encrypted email. ; The email is encrypted and sent within the SendEmail function. success = CkMailMan::ckSendEmail(mailman,email) If success <> 1 Debug CkMailMan::ckLastErrorText(mailman) CkEmail::ckDispose(email) CkCert::ckDispose(cert) CkMailMan::ckDispose(mailman) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndIf Debug "Encrypted email sent!" CkEmail::ckDispose(email) CkCert::ckDispose(cert) CkMailMan::ckDispose(mailman) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.