Public-key of the received signed mail can be stored as follows:
// load a mail or retrieve it using POP3 or IMAP
MailMessage receivedSignedMail = new MailMessage();
// store signer certificate
if (receivedSignedMail.Signers.Count > 0)
{
// get the first signer certificate
Certificate signerCert = receivedSignedMail.Signers[0].Certificate;
// save the certificate to a file
File.WriteAllBytes("path.cer", signerCert.GetRawCertData());
}
The saved Public-key can be load and used for encryption as follows:
// load certificate
Certificate cert = Certificate.LoadDer("path.cer");
// prepare a mail for encryption
MailMessage encryptedMailToSend = new MailMessage();
// ...
// encrypt the mail
encryptedMailToSend.Encrypt(cert);
For information about certificates, signing and encrypting please visit our blog or Wikipedia.