Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
IMAP Download and Verify Signed MIME
See more IMAP Examples
Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.Chilkat Delphi ActiveX Downloads
var
success: Integer;
imap: TChilkatImap;
sbMime: TChilkatStringBuilder;
mime: TChilkatMime;
alreadySavedParts: Integer;
st: TChilkatStringTable;
numFiles: Integer;
i: Integer;
email: TChilkatEmail;
cert: TChilkatCert;
begin
success := 0;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap := TChilkatImap.Create(Self);
// Connect to an IMAP server.
// Use TLS
imap.Ssl := 1;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = 0) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
success := imap.Login('myLogin','myPassword');
if (success = 0) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
// Select an IMAP mailbox
success := imap.SelectMailbox('Inbox');
if (success = 0) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
// Download the 1st email (as MIME) in the Inbox by sequence number.
sbMime := TChilkatStringBuilder.Create(Self);
success := imap.FetchSingleAsMimeSb(1,0,sbMime.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
// Load it into a MIME object and check to see if it is signed
mime := TChilkatMime.Create(Self);
mime.LoadMimeSb(sbMime.ControlInterface);
alreadySavedParts := 0;
if (mime.ContainsSignedParts() = 1) then
begin
// This will save the .p7s and other parts...
st := TChilkatStringTable.Create(Self);
success := mime.PartsToFiles('c:/temp/qa_output',st.ControlInterface);
if (success = 1) then
begin
numFiles := st.Count;
i := 0;
while i < numFiles do
begin
Memo1.Lines.Add('Created: ' + st.StringAt(i));
i := i + 1;
end;
alreadySavedParts := 1;
end;
end;
// Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
email := TChilkatEmail.Create(Self);
email.SetFromMimeSb(sbMime.ControlInterface);
if (email.ReceivedSigned = 1) then
begin
Memo1.Lines.Add('This email was signed.');
// Check to see if the signatures were verified.
if (email.SignaturesValid = 1) then
begin
Memo1.Lines.Add('Digital signature(s) verified.');
Memo1.Lines.Add('Signer: ' + email.SignedBy);
// The certificate used for signing may be obtained
// by calling email.GetSignedByCert.
cert := TChilkatCert.Create(Self);
success := email.LastSignerCert(i,cert.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add('Failed to get signing certificate object.');
end
else
begin
Memo1.Lines.Add('Signing cert: ' + cert.SubjectCN);
end;
end;
end
else
begin
Memo1.Lines.Add('Digital signature verification failed.');
end;
if (alreadySavedParts <> 1) then
begin
email.SaveAllAttachments('c:/temp/qa_output');
end;